我是Phalcon编程的新手,我有管理员/后端和前端控制器 管理员将在'/ admin /:controller /:action'提供,前端将在'/:controller /:action'提供
管理控制器(KalkuRitel \ Controllers \ Admin名称空间)位于
下app/
controllers/
admin/
和前端控制器(KalkuRitel \ Controllers \ Frontend命名空间)位于
下app/
controllers/
frontend/
我如何做到这一点? 以及如何使用自己的布局在admin和前端控制器中提供404页面?
答案 0 :(得分:1)
我建议创建模块
app/
modules/
admin/
...
frontend/
...
api/
...
注册模块:
$application->registerModules(array(
'frontend' => array(
'className' => 'Application\Frontend\Module',
'path' => __DIR__ . '/../modules/frontend/Module.php'
),
'admin' => array(
'className' => 'Application\Admin\Module',
'path' => __DIR__ . '/../modules/admin/Module.php'
),
'api' => array(
'className' => 'Application\Api\Module',
'path' => __DIR__ . '/../modules/api/Module.php'
)
));
定义正确的Module.php
文件,而不是设置路线有点接近:
use Phalcon\Mvc\Router as Router;
use Phalcon\CLI\Router as CliRouter;
/**
* Registering a router
*/
$di->setShared('router', function () use ($application, $config) {
if($application instanceof Phalcon\CLI\Console) {
return new CliRouter();
}
$router = new Router(false);
$router->setDefaultModule("frontend");
$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_GET_URL);
$router->removeExtraSlashes(TRUE);
foreach($application->getModules() as $key => $module) {
$prefix = $key == 'frontend' ? '' : ('/' . $key);
$router->add($prefix . '/:params', array(
'module' => $key,
'controller' => 'index',
'action' => 'index',
'params' => 1
))->setName($key);
$router->add($prefix . '/:controller/:params', array(
'module' => $key,
'controller' => 1,
'action' => 'index',
'params' => 2
));
$router->add($prefix . '/:controller/:action/:params', array(
'module' => $key,
'controller' => 1,
'action' => 2,
'params' => 3
));
}
return $router;
});
手册更多内容: https://docs.phalconphp.com/pl/latest/reference/routing.html和 https://docs.phalconphp.com/pl/latest/reference/applications.html
答案 1 :(得分:0)
实现此目标的最佳方法是创建一个多模块应用程序。
您可以在此处详细了解多个模块应用程序设置: https://docs.phalconphp.com/en/latest/reference/applications.html#multi-module
一旦你有了这个结构,你可以设置这样的路线:
/*
* Dependency Injector
*/
$di = new \Phalcon\Di\FactoryDefault();
/**
* Register a router
*/
$di->setShared('router', function () {
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule('frontend');
$router->setDefaultNamespace('Multiple\Frontend\Controllers');
/*
* Frontend Routes
*/
$router->add('/:controller/:action', array(
'namespace' => 'Multiple\Frontend\Controllers',
'module' => 'frontend',
'controller' => 1,
'action' => 2
));
/*
* Backend Routes
*/
$backendRoutes = new \Phalcon\Mvc\Router\Group(array(
'namespace' => 'Multiple\Backend\Controllers',
'module' => 'backend'
));
$backendRoutes->setPrefix('/admin');
$backendRoutes->add('/:controller/:action', array(
'controller' => 1,
'action' => 3
));
$router->mount($backendRoutes);
return $router;
});
这是一个快速的答案,可能需要根据您的具体情况进行调整,但应该让您了解如何实现目标。