2017-11-20 6 views
1

これは私のログインページのURLです:http://localhost/multi_shopping/PanelAdmin/Users/indexです。 ログイン後、次のURL、http://localhost/multi_shopping/PanelAdmin/categories/homeにリダイレクトされています。私は彼がログインしていない場合、このURLにアクセスすることができないようにユーザを制限しましたが、http://localhost/multi_shopping/PanelAdmin/categories/homeこのURLをクリックすると、http://localhost/multi_shopping/users/login?redirect=%2FPanelAdmin%2FCategories%2Fhomeにリダイレクトされました。エラー:UsersControllerは見つかりませんでした。プラグインPanelAdminディレクトリにあります。ログインせずにカテゴリページにアクセスしようとすると、ログインページにリダイレクトする必要があります。私の問題を整理するのを助けてください。Cakephp 3で許可されていないアクションへのアクセスが間違っています

Code snippet:

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 Plugin File

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

routes.php Application Route File

<?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(); 
+0

を$ this-> loadComponent配列。 –

+0

@ManoharKhadka:コメントを付けるが同じ問題。routes.phpファイルを追加しました – user3653474

+0

route.phpコードを完全に投稿しますか? –

答えて

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'); 
}); 

以下を追加し、次の行コメントすることができ:ちょうど内「プレフィックス」=> falseオプションを削除/でコメント

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

エラー:PanelAdminControllerがアクセスしようとしたときにエラーが発生しました任意のコントローラーアクション。 PanelAdminは、コントローラではないプラグインである必要がありますroutes.phpファイル – user3653474

+0

同じ問題http:// localhost/multi_shopping/users/login?redirect =%2FPanelAdmin%2Fcategories%2Fhomeアクセス時にこのURLにリダイレクトされました..... ./PanelAdmincategories/home – user3653474

+0

PanelAdminはプラグイン内に直接配置されていますか? –

関連する問題