|
7 | 7 | use Phalcon\Mvc\Url as UrlProvider; |
8 | 8 | use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; |
9 | 9 |
|
10 | | - |
| 10 | +define('BASE_PATH', dirname(__DIR__)); |
| 11 | +define('APP_PATH', BASE_PATH . '/app'); |
11 | 12 |
|
12 | 13 | // Register an autoloader |
13 | 14 | $loader = new Loader(); |
14 | | - |
15 | 15 | $loader->registerDirs( |
16 | | - [ |
17 | | - "../app/controllers/", |
18 | | - "../app/models/", |
19 | | - ] |
20 | | -); |
21 | | - |
22 | | -$loader->register(); |
23 | | - |
24 | | - |
| 16 | + array( |
| 17 | + APP_PATH . '/controllers/', |
| 18 | + APP_PATH . '/models/' |
| 19 | + ) |
| 20 | +)->register(); |
25 | 21 |
|
26 | 22 | // Create a DI |
27 | 23 | $di = new FactoryDefault(); |
28 | 24 |
|
29 | | -// Setup the view component |
30 | | -$di->set( |
31 | | - "view", |
32 | | - function () { |
33 | | - $view = new View(); |
34 | | - |
35 | | - $view->setViewsDir("../app/views/"); |
36 | | - |
37 | | - return $view; |
38 | | - } |
39 | | -); |
| 25 | +// Setting up the view component |
| 26 | +$di['view'] = function() { |
| 27 | + $view = new View(); |
| 28 | + $view->setViewsDir(APP_PATH . '/views/'); |
| 29 | + return $view; |
| 30 | +}; |
40 | 31 |
|
41 | 32 | // Setup a base URI so that all generated URIs include the "tutorial" folder |
42 | | -$di->set( |
43 | | - "url", |
44 | | - function () { |
45 | | - $url = new UrlProvider(); |
46 | | - |
47 | | - $url->setBaseUri("/tutorial/"); |
48 | | - |
49 | | - return $url; |
50 | | - } |
51 | | -); |
52 | | - |
53 | | - |
54 | | - |
55 | | -$application = new Application($di); |
56 | | - |
| 33 | +$di['url'] = function() { |
| 34 | + $url = new Url(); |
| 35 | + $url->setBaseUri('/'); |
| 36 | + return $url; |
| 37 | +}; |
| 38 | + |
| 39 | +// Set the database service |
| 40 | +$di['db'] = function() { |
| 41 | + return new DbAdapter(array( |
| 42 | + "host" => "127.0.0.1", |
| 43 | + "username" => "root", |
| 44 | + "password" => "secret", |
| 45 | + "dbname" => "tutorial1" |
| 46 | + )); |
| 47 | +}; |
| 48 | + |
| 49 | +// Handle the request |
57 | 50 | try { |
58 | | - // Handle the request |
59 | | - $response = $application->handle(); |
60 | | - |
61 | | - $response->send(); |
62 | | -} catch (\Exception $e) { |
63 | | - echo "Exception: ", $e->getMessage(); |
| 51 | + $application = new Application($di); |
| 52 | + echo $application->handle()->getContent(); |
| 53 | +} catch (Exception $e) { |
| 54 | + echo "Exception: ", $e->getMessage(); |
64 | 55 | } |
0 commit comments