10
votes

I am not experienced with cakephp ver3.1.3

I followed the instructions to implement login authentication function; http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html

I managed to successfully cake bake my cakephp v3.1.3 app.

I have a problem with UsersController.php

I have the following code copied and pasted from http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-auth-example/auth.html;

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    // Allow users to register and logout.
    // You should not add the "login" action to allow list. Doing so would
    // cause problems with normal functioning of AuthComponent.
    $this->Auth->allow(['add', 'logout']);
} //public function beforeFilter(Event $event)

The presence of this code created the error below;

Strict (2048): Declaration of App\Controller\UsersController::beforeFilter() should be compatible with App\Controller\AppController::beforeFilter(Cake\Event\Event $event) [APP/Controller\UsersController.php, line 12] Code Context include - APP/Controller\UsersController.php, line 12 Composer\Autoload\includeFile - ROOT\vendor\composer\ClassLoader.php, line 412 Composer\Autoload\ClassLoader::loadClass() - ROOT\vendor\composer\ClassLoader.php, line 301 spl_autoload_call - [internal], line ?? class_exists - [internal], line ?? Cake\Core\App::_classExistsInBase() - CORE\src\Core\App.php, line 89 Cake\Core\App::className() - CORE\src\Core\App.php, line 66 Cake\Routing\Filter\ControllerFactoryFilter::_getController() - CORE\src\Routing\Filter\ControllerFactoryFilter.php, line 81 Cake\Routing\Filter\ControllerFactoryFilter::beforeDispatch() - CORE\src\Routing\Filter\ControllerFactoryFilter.php, line 49 Cake\Routing\DispatcherFilter::handle() - CORE\src\Routing\DispatcherFilter.php, line 145 Cake\Event\EventManager::_callListener() - CORE\src\Event\EventManager.php, line 389 Cake\Event\EventManager::dispatch() - CORE\src\Event\EventManager.php, line 355 Cake\Routing\Dispatcher::dispatchEvent() - CORE\src\Event\EventDispatcherTrait.php, line 78 Cake\Routing\Dispatcher::dispatch() - CORE\src\Routing\Dispatcher.php, line 62 [main] - ROOT\webroot\index.php, line 37

If I remove the offending code segment, the error disappears. What is wrong with the code segment?

2
Read the error message. It already tells you pretty clearly what is wrong. If you still can't understand it try to search for it. This is a standard php message and has been asked a felt 1000 times before on Stackoverflow. I've voted to close it as duplicate for that reason.floriank
This is not a CakePHP issue, but a PHP one. As the error message states your inherited method is not compatible with its parent.drmonkeyninja
It is not related to CakePHP. This is simply how php works. If you would have searched for the error, you would have realized this gets asked for Wordpress, Joomla and a lot other scripts. You still have not understood what the error means and why it happens. You probably don't understand how namespaces work either. Let me rephrase the error messages: Make the method signatures match.floriank
To copy and pasting code without understanding it is a very bad habit. It won't gain you anything and it will, in the worst case, introduce serious issues in a script. Just read the manuals, php and CakePHP have awesome documentation, especially compared to many other things, you just have to read it. I strongly recommend you to change your habit and learn how to learn before learning programming.floriank

2 Answers

29
votes

The solution is to add the following line to UsersController.php

use Cake\Event\Event;

The cake baked version does not have the above line.

6
votes

On Cake 4.0.2 Strawberry - it is now -

use Cake\Event\EventInterface;

public function beforeFilter(EventInterface  $event)
        {
            parent::beforeFilter($event);
    }