CakePHP: allow () member function call

ProtectedController.php

<?php class ProtectedController extends AppController { public $components = array( 'Session', 'Auth' => array( 'loginRedirect' => array('controller' => 'home', 'action' => 'index'), 'logoutRedirect' => array('controller' => 'home', 'action' => 'index') ) ); public $paginate = array( 'limit' => 2 ); public function beforeFilter() { $this->Auth->allow('index', 'view'); } } 

Appcontroller

 App::uses('Controller', 'Controller'); class AppController extends Controller { } 

UsersController

 <?php App::uses('ProtectedController', 'Controller'); class UsersController extends ProtectedController { public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow('add', 'logout'); } } 

I had

 Fatal Error Error: Call to a member function allow() on a non-object File: /Library/WebServer/Documents/cakephp_stats/app/Controller/ProtectedController.php Line: 18 Notice: If you want to customize this error message, create app/View/Errors/fatal_error.ctp 

.

Is there anyone how to solve this. From what I see, it should load the component in the ProtectedController, and AuthComponent will be loaded.

EDIT:

line 18 is the ProtectedController:

 public function beforeFilter() { $this->Auth->allow('index', 'view'); } 

EDIT:

You can fix this only now:

 public $components = array( 'Session', 'Auth' => array( 'loginRedirect' => array('controller' => 'home', 'action' => 'index'), 'logoutRedirect' => array('controller' => 'home', 'action' => 'index') ) ); 

in AppController then override and then allow everyone:

 class AppController extends Controller { public $components = array( 'Session', 'Auth' => array( 'loginRedirect' => array('controller' => 'home', 'action' => 'index'), 'logoutRedirect' => array('controller' => 'home', 'action' => 'index') ) ); public function beforeFilter() { $this->Auth->allow('*'); } } 
+4
source share
1 answer

I do like that. The main differences from your controllers:

  • My AppController includes the Auth component, as well as $this->Auth->allow() in beforeFilter .
  • My ProtectedController does not specify the login / logoutRedirects and calls its parent beforeFilter .
  • My UsersController does not extend the ProtectedController at all. My protected controllers enable the Auth component and call their parent beforeFilter .

Just speculate: the Auth component by default redirects / users / login if an unauthorized person tries to access a protected resource. Since you are protecting the UserController and not allowing login actions, this may be causing the behavior you are experiencing.

+1
source

Source: https://habr.com/ru/post/1440682/


All Articles