Cakephp3中未经授权的操作访问重定向错误



这是我的登录页面url:http://localhost/multi_shopping/PanelAdmin/Users/index.登录后,我被重定向到以下网址,即。http://localhost/multi_shopping/PanelAdmin/categories/home.我已经限制用户不能访问这个网址,如果他没有登录,但如果我点击http://localhost/multi_shopping/PanelAdmin/categories/home这个url然后我被重定向到http://localhost/multi_shopping/users/login?redirect=%2FPanelAdmin%2FCategories%2Fhome,显示错误消息:错误:找不到UsersController,但我正在插件PanelAdmin目录中工作。若用户尝试在不登录的情况下访问类别页面,我希望他重定向到登录页面。请帮我解决一下问题。

代码片段:

AppController.php

$this->loadComponent('Auth', [
'authorize'=> 'Controller',
'authenticate' => [
'Form' => [
// fields used in login form
'fields' => [
'username' => 'username',
'password' => 'password'
]
]
],
'loginRedirect' => [
'controller' => 'Categories',
'action' => 'home'
],
'logoutRedirect' => [
'controller' => 'users',
'action' => 'index'
],
'unauthorizedRedirect' => [
'controller' => 'users',
'action' => 'index',//,
'prefix' => false
//'home'
],
'authError' => 'Did you really think you are allowed to see that?',
]);

UsersController.php

public function login()
{   
if ($this->request->is('post')) {
$user = $this->Auth->identify();
//debug($user); die;
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}

类别Controller.php

public function isAuthorized($user)
{   
$action = $this->request->params['action'];
//  registered users can add topics and view index
if (in_array($action, ['home'])) {
return true;
}
// All other actions require an id or users cannot do it
if (empty($this->request->params['pass'][0])) {
return false;
}      
return parent::isAuthorized($user);
}

routes.php插件文件

Router::plugin(
'PanelAdmin',
['path' => '/PanelAdmin'],
function (RouteBuilder $routes) {
$routes->fallbacks(DashedRoute::class);
}
);

routes.php应用程序路由文件

<?php
/**
* Routes configuration
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different URLs to chosen controllers and their actions (functions).
*
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link          https://cakephp.org CakePHP(tm) Project
* @license       https://opensource.org/licenses/mit-license.php MIT License
*/
use CakeCorePlugin;
use CakeRoutingRouteBuilder;
use CakeRoutingRouter;
use CakeRoutingRouteDashedRoute;
/**
* The default class to use for all routes
*
* The following route classes are supplied with CakePHP and are appropriate
* to set as the default:
*
* - Route
* - InflectedRoute
* - DashedRoute
*
* If no call is made to `Router::defaultRouteClass()`, the class used is
* `Route` (`CakeRoutingRouteRoute`)
*
* Note that `Route` does not do any inflections on URLs which will result in
* inconsistently cased URLs when used with `:plugin`, `:controller` and
* `:action` markers.
*
*/
Router::defaultRouteClass(DashedRoute::class);
Router::scope('/', function (RouteBuilder $routes) {
/**
* Here, we are connecting '/' (base path) to a controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, src/Template/Pages/home.ctp)...
*/
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
/**
* ...and connect Admin Panel URLs.
*/
$routes->connect('/PanelAdmin', ['plugin' => 'PanelAdmin', 'controller' => 'Users','action' => 'index']);


/**
* Connect catchall routes for all controllers.
*
* Using the argument `DashedRoute`, the `fallbacks` method is a shortcut for
*    `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);`
*    `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);`
*
* Any route class can be used with this method, such as:
* - DashedRoute
* - InflectedRoute
* - Route
* - Or your own route class
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$routes->fallbacks(DashedRoute::class);
});
/**
* Load all plugin routes. See the Plugin documentation on
* how to customize the loading of plugin routes.
*/
Plugin::routes();

尝试在routes.php文件中添加适当的前缀,在Plugin::routes();上方添加以下内容

Router::prefix('PanelAdmin', function ($routes) {
// All routes here will be prefixed with `/admin`
// And have the prefix => admin route element added.
$routes->extensions(['json', 'xml', 'ajax']);
$routes->connect('/', ['controller' => 'Users', 'action' => 'login']);
$routes->fallbacks('DashedRoute');
});

您可以评论以下行:

// $routes->connect('/PanelAdmin', ['plugin' => 'PanelAdmin', 'controller' => 'Users','action' => 'index']);

最新更新