Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Uncaught Error: Call to a member function read() on boolean for cookies #12

Open
anuj9196 opened this issue Jan 31, 2017 · 1 comment

Comments

@anuj9196
Copy link

This is how I have setup my AppController.php, but it is giving error

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Core\Configure;

class AppController extends Controller
{
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Dashboard',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'home'
            ],
            'authenticate' => [
                'Form' => [
                    'finder' => 'auth',
                ],
                'Xety/Cake3CookieAuth.Cookie' => [
                    'cookie' => [
                        'name' => 'CookieAuth'
                    ]
                ]
            ]
        ]);

        //$this->loadComponent('Security');
        //$this->loadComponent('Csrf');
    }

    public function beforeRender(Event $event)
    {
        $this->loadComponent('Auth');

        // login using cookies
        if (!$this->Auth->user('id') && $this->Cookie->read('CookieAuth')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
            } else {
                $this->Cookie->delete('CookieAuth');
            }
        }

        // set logged in user data to render in view files
        if ($this->Auth->user('id')) {
            $this->set('AUTH_USER', $this->Auth->user());
        }

        if (!array_key_exists('_serialize', $this->viewVars) &&
            in_array($this->response->type(), ['application/json', 'application/xml'])
        ) {
            $this->set('_serialize', true);
        }
    }
}

Error getting is

Fatal error: Uncaught Error: Call to a member function read() on boolean in /var/www/html/emeet.com/emeet_cake/src/Controller/AppController.php:46

on removing $this->Cookie->read('CookieAuth'), error jumped to next Cookie->delete line

@wgnahs
Copy link

wgnahs commented Jun 23, 2017

Better late than never, but it might be useful for someone else. You forgot to add public $components = ['Cookie']; in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants