Skip to content

Commit 799b035

Browse files
committed
Tidied up the Multiple Shared Views example.
1 parent b924375 commit 799b035

File tree

6 files changed

+133
-90
lines changed

6 files changed

+133
-90
lines changed

multiple-shared-views/apps/modules/backend/Module.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
class Module
66
{
7-
87
public function registerAutoloaders()
98
{
10-
119
$loader = new \Phalcon\Loader();
1210

13-
$loader->registerNamespaces(array(
14-
'Multiple\Backend\Controllers' => '../apps/modules/backend/controllers/',
15-
'Multiple\Backend\Models' => '../apps/modules/backend/models/'
16-
));
11+
$loader->registerNamespaces(
12+
[
13+
"Multiple\\Backend\\Controllers" => "../apps/modules/backend/controllers/",
14+
"Multiple\\Backend\\Models" => "../apps/modules/backend/models/",
15+
]
16+
);
1717

1818
$loader->register();
1919
}
@@ -25,20 +25,32 @@ public function registerServices($di)
2525
{
2626

2727
//Registering a dispatcher
28-
$di->set('dispatcher', function () {
29-
$dispatcher = new \Phalcon\Mvc\Dispatcher();
30-
$dispatcher->setDefaultNamespace("Multiple\Backend\Controllers\\");
31-
return $dispatcher;
32-
});
28+
$di->set(
29+
"dispatcher",
30+
function () {
31+
$dispatcher = new \Phalcon\Mvc\Dispatcher();
32+
33+
$dispatcher->setDefaultNamespace(
34+
"Multiple\Backend\Controllers\\"
35+
);
36+
37+
return $dispatcher;
38+
}
39+
);
3340

3441
//Set a different connection in each module
35-
$di->set('db', function () {
36-
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
37-
"host" => "localhost",
38-
"username" => "root",
39-
"password" => "secret",
40-
"dbname" => "invo"
41-
));
42-
});
42+
$di->set(
43+
"db",
44+
function () {
45+
return new \Phalcon\Db\Adapter\Pdo\Mysql(
46+
[
47+
"host" => "localhost",
48+
"username" => "root",
49+
"password" => "secret",
50+
"dbname" => "invo",
51+
]
52+
);
53+
}
54+
);
4355
}
4456
}

multiple-shared-views/apps/modules/backend/controllers/IndexController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
class IndexController extends Controller
88
{
9-
109
public function indexAction()
1110
{
12-
return $this->dispatcher->forward('login');
11+
return $this->dispatcher->forward("login");
1312
}
1413
}

multiple-shared-views/apps/modules/backend/controllers/ProductsController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class ProductsController extends Controller
99
{
10-
1110
public function indexAction()
1211
{
1312
$this->view->product = Products::findFirst();

multiple-shared-views/apps/modules/frontend/Module.php

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
class Module
66
{
7-
87
public function registerAutoloaders()
98
{
10-
119
$loader = new \Phalcon\Loader();
1210

13-
$loader->registerNamespaces(array(
14-
'Multiple\Frontend\Controllers' => '../apps/modules/frontend/controllers/',
15-
'Multiple\Frontend\Models' => '../apps/modules/frontend/models/',
16-
));
11+
$loader->registerNamespaces(
12+
[
13+
"Multiple\\Frontend\\Controllers" => "../apps/modules/frontend/controllers/",
14+
"Multiple\\Frontend\\Models" => "../apps/modules/frontend/models/",
15+
]
16+
);
1717

1818
$loader->register();
1919
}
@@ -23,21 +23,30 @@ public function registerAutoloaders()
2323
*/
2424
public function registerServices($di)
2525
{
26-
27-
//Registering a dispatcher
28-
$di->set('dispatcher', function () {
29-
$dispatcher = new \Phalcon\Mvc\Dispatcher();
30-
$dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
31-
return $dispatcher;
32-
});
33-
34-
$di->set('db', function () {
35-
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
36-
"host" => "localhost",
37-
"username" => "root",
38-
"password" => "secret",
39-
"dbname" => "invo"
40-
));
41-
});
26+
// Registering a dispatcher
27+
$di->set(
28+
"dispatcher",
29+
function () {
30+
$dispatcher = new \Phalcon\Mvc\Dispatcher();
31+
32+
$dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
33+
34+
return $dispatcher;
35+
}
36+
);
37+
38+
$di->set(
39+
"db",
40+
function () {
41+
return new \Phalcon\Db\Adapter\Pdo\Mysql(
42+
[
43+
"host" => "localhost",
44+
"username" => "root",
45+
"password" => "secret",
46+
"dbname" => "invo",
47+
]
48+
);
49+
}
50+
);
4251
}
4352
}

multiple-shared-views/apps/modules/frontend/controllers/IndexController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
class IndexController extends Controller
88
{
9-
109
public function indexAction()
1110
{
1211
}

multiple-shared-views/public/index.php

Lines changed: 70 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,89 @@
44
use Phalcon\Mvc\View;
55
use Phalcon\Mvc\Application;
66
use Phalcon\DI\FactoryDefault;
7+
use Multiple\Frontend\Module as FrontendModule;
8+
use Multiple\Backend\Module as BackendModule;
79

810
error_reporting(E_ALL);
911

1012
$di = new FactoryDefault();
1113

12-
//Registering a router
13-
$di->set('router', function () {
14+
// Registering a router
15+
$di->set(
16+
"router",
17+
function () {
18+
$router = new Router();
1419

15-
$router = new Router();
20+
$router->setDefaultModule("frontend");
1621

17-
$router->setDefaultModule("frontend");
22+
$router->add(
23+
"/:controller/:action",
24+
[
25+
"module" => "frontend",
26+
"controller" => 1,
27+
"action" => 2,
28+
]
29+
);
1830

19-
$router->add('/:controller/:action', array(
20-
'module' => 'frontend',
21-
'controller' => 1,
22-
'action' => 2,
23-
));
31+
$router->add(
32+
"/login",
33+
[
34+
"module" => "backend",
35+
"controller" => "login",
36+
"action" => "index",
37+
]
38+
);
2439

25-
$router->add("/login", array(
26-
'module' => 'backend',
27-
'controller' => 'login',
28-
'action' => 'index',
29-
));
40+
$router->add(
41+
"/admin/products/:action",
42+
[
43+
"module" => "backend",
44+
"controller" => "products",
45+
"action" => 1,
46+
]
47+
);
3048

31-
$router->add("/admin/products/:action", array(
32-
'module' => 'backend',
33-
'controller' => 'products',
34-
'action' => 1,
35-
));
49+
$router->add(
50+
"/products/:action",
51+
[
52+
"module" => "frontend",
53+
"controller" => "products",
54+
"action" => 1,
55+
]
56+
);
3657

37-
$router->add("/products/:action", array(
38-
'module' => 'frontend',
39-
'controller' => 'products',
40-
'action' => 1,
41-
));
58+
return $router;
59+
}
60+
);
4261

43-
return $router;
44-
});
62+
// Registering a shared view component
63+
$di->set(
64+
"view",
65+
function () {
66+
$view = new View();
4567

46-
//Registering a shared view component
47-
$di->set('view', function () {
48-
$view = new View();
49-
$view->setViewsDir('../apps/common/views/');
50-
return $view;
51-
});
68+
$view->setViewsDir("../apps/common/views/");
69+
70+
return $view;
71+
}
72+
);
5273

5374
$application = new Application($di);
5475

55-
//Register the installed modules
56-
$application->registerModules(array(
57-
'frontend' => array(
58-
'className' => 'Multiple\Frontend\Module',
59-
'path' => '../apps/modules/frontend/Module.php'
60-
),
61-
'backend' => array(
62-
'className' => 'Multiple\Backend\Module',
63-
'path' => '../apps/modules/backend/Module.php'
64-
)
65-
));
66-
67-
echo $application->handle()->getContent();
76+
// Register the installed modules
77+
$application->registerModules(
78+
[
79+
"frontend" => [
80+
"className" => FrontendModule::class,
81+
"path" => "../apps/modules/frontend/Module.php",
82+
],
83+
"backend" => [
84+
"className" => BackendModule::class,
85+
"path" => "../apps/modules/backend/Module.php",
86+
],
87+
]
88+
);
89+
90+
$response = $application->handle();
91+
92+
echo $response->getContent();

0 commit comments

Comments
 (0)