質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

Q&A

解決済

1回答

3814閲覧

cakephpで認証したら他のページにアクセスできない。

ganariya

総合スコア50

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

0グッド

0クリップ

投稿2018/09/21 08:40

編集2018/09/21 12:25

前提・実現したいこと

現在、ソースコードの共有アプリケーションを作成しようと思います。
UsersControllerを作成して認証を作成しました。
そうすると、
cake/users/indexなどにはアクセスできるのですが
cake/index.phpや
cake/Home/index.phpにアクセスできなくなってしまいました。
ほかのページに移動できるようにするにはどうすればよいでしょうか。

発生している問題・エラーメッセージ

usersコントローラを作ったあと、他のディレクトリにアクセスできなくなり、
you are not authorized to access that location
と表示されて、もとのusersページに移されてしまいます。

具体的には
C/xampp/htdocs/cake/src/Controller/UsersController(localhost/cake/Users/index)にはアクセスできますが
C/xampp/htdocs/cake/src/Controller/HomeController(localhost/cake/Home/index)や
localhost/cake/index.php(一番最初のデータベースがつながったかどのデータベース)にアクセスできません。

該当のソースコード

UsersController

php

1<?php 2namespace App\Controller; 3 4use App\Controller\AppController; 5use Cake\Event\Event; 6 7/** 8 * Users Controller 9 * 10 * @property \App\Model\Table\UsersTable $Users 11 * 12 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) 13 */ 14class UsersController extends AppController 15{ 16 public function beforeFilter(Event $event) 17 { 18 parent::beforeFilter($event); 19 20 //addとlogoutでは認証いらないよ! 21 $this->Auth->allow(['add', 'logout']); 22 } 23 24 public function login() 25 { 26 //postなら 27 if ($this->request->is('post')) { 28 29 //POStされた内容からデータベースを検索 30 $user = $this->Auth->identify(); 31 32 //成功したら 33 if ($user) { 34 35 //セッションにユーザー情報を保持 36 //保存したデータは$this->Auth->userで取得できる 37 $this->Auth->setUser($user); 38 39 //AppControllerで選択したURLへ 40 return $this->redirect($this->Auth->redirectUrl()); 41 } 42 43 //失敗したらFlashを表示 44 $this->Flash->error(__('Invalid username or password, try again')); 45 } 46 } 47 48 public function logout() 49 { 50 $this->request->session()->destroy(); 51 return $this->redirect($this->Auth->logout()); 52 } 53 54 public function isAuthorized($user) 55 { 56 return true; 57 } 58 59 public function index() 60 { 61 $this->set('users', $this->Users->find('all')); 62 } 63 64 public function view($id) 65 { 66 $user = $this->Users->get($id); 67 $this->set(compact('user')); 68 } 69 70 public function add() 71 { 72 //実体を作る 73 $user = $this->Users->newEntity(); 74 75 //POST処理なら 76 if ($this->request->is('post')) { 77 78 //データを更新するときに使う 79 $user = $this->Users->patchEntity($user, $this->request->getData()); 80 81 //追加できたら 82 if ($this->Users->save($user)) { 83 $this->Flash->success(__('The user has been saved.')); 84 85 //リダイレクト 86 return $this->redirect(['action' => 'login']); 87 } 88 $this->Flash->error(__('Unable to add the user.')); 89 } 90 91 //userを設定する 92 $this->set('user', $user); 93 } 94 95} 96

AppController

php

1<?php 2/** 3 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 4 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 5 * 6 * Licensed under The MIT License 7 * For full copyright and license information, please see the LICENSE.txt 8 * Redistributions of files must retain the above copyright notice. 9 * 10 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 11 * @link https://cakephp.org CakePHP(tm) Project 12 * @since 0.2.9 13 * @license https://opensource.org/licenses/mit-license.php MIT License 14 */ 15namespace App\Controller; 16 17use Cake\Controller\Controller; 18use Cake\Event\Event; 19 20/** 21 * Application Controller 22 * 23 * Add your application-wide methods in the class below, your controllers 24 * will inherit them. 25 * 26 * @link https://book.cakephp.org/3.0/en/controllers.html#the-app-controller 27 */ 28class AppController extends Controller 29{ 30 31 /** 32 * Initialization hook method. 33 * 34 * Use this method to add common initialization code like loading components. 35 * 36 * e.g. `$this->loadComponent('Security');` 37 * 38 * @return void 39 */ 40 public function initialize() 41 { 42 parent::initialize(); 43 44 $this->loadComponent('RequestHandler', [ 45 'enableBeforeRedirect' => false, 46 ]); 47 $this->loadComponent('Flash'); 48 49 //認証について 50 $this->loadComponent('Auth', [ 51 'authorize' => ['Controller'], 52 53 //ログイン後のアクション指定 54 'loginRedirect' => [ 55 'controller' => 'Users', 56 'action' => 'index' 57 ], 58 59 //ログアウト後の遷移先 60 'logoutRedirect' => [ 61 'controller' => 'Users', 62 'action' => 'login' 63 ] 64 ]); 65 } 66 67 public function isAuthorized($user){ 68 return false; 69 } 70} 71 72 73routes.php 74
<?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. * * Cache: Routes are cached to improve performance, check the RoutingMiddleware * constructor in your `src/Application.php` file to change this behavior. * */ 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']); /** * 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); }); ``` ``` ### 試したこと 考えている原因として、 UsersControllerのbeforeFilterメソッドで行っている $this->Auth->allowで認証しなくてよいページを定めているため 他のページにアクセスできていないきがします。 どうすれば、他のページにアクセスできるように変更できるでしょうか。 ### 補足情報(FW/ツールのバージョンなど) cakephp3 ### 追記 申し訳ありません。 routes.phpというものが大事だったのですね。 routes.phpを書き換えれば、Usersコントローラーの認証は関係なくなるのでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2018/09/21 09:32

ルーティングはどのように設定されているのでしょうか。 https://book.cakephp.org/3.0/ja/development/routing.html フレームワーク配下のファイルであればルールに則ってアクセスしないと幾ら直でURLを実行しようとしても無理かと思います。
guest

回答1

0

ベストアンサー

HomeControllerはAppControllerを継承していますか?
AppContoroller#isAuthorizedがfalseを返すようになっているので、AppControllerを継承したコントローラーはログイン状態に関係なくアクセスできなくなっているのだと思います。

UsersControllerにアクセスできるのは、isAuthorizedをオーバーライドしtrueを返すようになっているからです。

認証(Authentication)と認可(Authorization)は別ものですので、そこを理解できると解決できると思います。

投稿2018/09/22 03:51

uran

総合スコア46

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問