我正在尝试使用前缀" student"。当我在模板或布局文件中创建链接时,我收到此错误,如图所示:
routes.php中的代码
<?php
use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;
Router::defaultRouteClass(DashedRoute::class);
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks(DashedRoute::class);
});
Router::prefix('admin', function ($routes) {
$routes->connect('/users/', [ 'controller' => 'MyUsers', 'action' => 'index', 'plugin'=>false]);
$routes->connect('/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false, 'prefix'=>'admin']);
$routes->connect('/', [ 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin'=>false]);
$routes->fallbacks(DashedRoute::class);
});
Router::prefix('trainer', function ($routes) {
$routes->connect('/users/', [ 'controller' => 'MyUsers', 'action' => 'index', 'plugin'=>false]);
$routes->connect('/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false]);
$routes->connect('/', [ 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin'=>false]);
$routes->fallbacks(DashedRoute::class);
});
Router::prefix('student', function ($routes) {
$routes->connect('/courses/', array ( 'controller' => 'Courses', 'action' => 'index', 'plugin' => false, 'prefix' => 'student', '_ext' => NULL, ));
$routes->connect('/', array ( 'controller' => 'MyUsers', 'action' => 'dashboard', 'plugin' => false));
});
/**
* Load all plugin routes. See the Plugin documentation on
* how to customize the loading of plugin routes.
*/
Plugin::routes();
布局文件student.ctp只有一行代码:
<li><?php echo $this->Html->link('Courses', [ 'controller' => 'Courses', 'action' => 'index', 'plugin' => false, 'prefix' => 'student', '_ext' => NULL, ]);?></li>
AppController.php:
<?php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
class AppController extends Controller
{
public $helpers = array(
'CakeDC/Users.AuthLink',
'CakeDC/Users.User',
);
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('CakeDC/Users.UsersAuth');
$this->loadComponent('Utils.GlobalAuth');
$this->Auth->config('loginRedirect', array('controller'=>'Courses', 'action'=>'index', 'plugin'=>FALSE));
$this->Auth->config('logoutRedirect', array('controller'=>'MyUsers', 'action'=>'login', 'plugin'=>FALSE));
$this->Auth->config('unauthorizedRedirect', array('controller'=>'Courses', 'action'=>'index', 'prefix'=>$this->Auth->user('role')));
$this->Auth->config('loginAction', array('controller'=>'MyUsers', 'action'=>'login'));
$this->Auth->allow(['login', 'logout']);
}
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
$this->_renderLayout();
}
private function _renderLayout()
{
$prefix = isset($this->request->params['prefix'])?$this->request->params['prefix']:FALSE;
if(!$prefix)
{
return;
}
$this->viewBuilder()->setLayout($prefix);
}
}
我已经检查了这个解决方案:CakePHP 3: Missing route error for route that exists
答案 0 :(得分:2)
您无法使用布尔值提供特殊plugin
键,它必须是null
或带有插件名称的字符串。
此外,在连接路由时无需定义plugin
或prefix
键,Router::prefix()
方法将负责添加前缀。类似Router::plugin()
将添加插件名称,如果不使用Router::plugin()
,则会假定null
密钥的默认值为plugin
。
如果您想禁止生成带扩展名的网址,那么仅使用_ext
定义null
才有意义。在生成URL时指定它只是在将其定义为非null
值时才需要,对于plugin
键也是如此(除非您需要突破当前插件上下文)
长话短说,连接路线只需要controller
和action
键:
$routes->connect('/courses/', [
'controller' => 'Courses',
'action' => 'index'
]);
生成网址只需要额外的prefix
密钥,如果在插件上下文中没有使用,则plugin
是可选的:
$this->Html->link('Courses', [
'controller' => 'Courses',
'action' => 'index',
'plugin' => null,
'prefix' => 'student'
]);
另见