在Cakephp 3中错误重定向未经授权的操作访问

时间:2017-11-20 06:06:06

标签: php cakephp cakephp-3.0

这是我的登录页面网址:http://localhost/multi_shopping/PanelAdmin/Users/index。 登录后,我被重定向到以下网址,即http://localhost/multi_shopping/PanelAdmin/categories/home。如果他没有登录,我限制用户无法访问此网址,但如果我点击http://localhost/multi_shopping/PanelAdmin/categories/home此网址,则我已被重定向到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'));
        }
    }
  

CategoriesController.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 Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
use Cake\Routing\Route\DashedRoute;

/**
 * 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` (`Cake\Routing\Route\Route`)
 *
 * 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();

1 个答案:

答案 0 :(得分:0)

尝试在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']);