user4458505 user4458505 - 4 months ago 9
PHP Question

How to modify the appearance of auth related flash messages?

I am having no luck trying to change the Flash Element on the AuthError from default to error?

I was just trying to see if I can change it, but now its driving me up the wall as I can not seem to change it?

This is how I have loaded my Auth in the AppController,

$this->loadComponent('Auth', [
'authError' => 'Did you really think you are allowed to see that? -2',
'authenticate' => [
'Form' => [
'fields' => ['username' => 'email', 'password' => 'password']
]
],
'loginAction' => [
'controller' => 'Users',
'action' => 'Login'
],
'loginRedirect' => [
'controller' => 'Pages',
'action' => 'LoginPage'
],
'logoutRedirect' => [
'controller' => 'Pages',
'action' => 'HomePage'
]
]);


So when I go to a not allowed page, it displays the authError message but using whatever class/id's are in the
Element/Flash/default.ctp
I wanted to change it to just use the same as the
error.cpt


I have debugged the Auth Component, there was a 'flash' setting, tried setting that, but it did not work?

So how do I change the authError to use a different Flash Layout?

Thanks,

ndm ndm
Answer

As you've figured, auth messages are using the default.ctp element by default, and that it's possible to configure the flash element when rendering it directly. To affect this globally, you can configure the component instead.

If all you want to do is to change the classname, then you can use the class parameter in the flash configuration options params setting:

$this->loadComponent('Auth', [
    // ...
    'flash' => [
        'params' => [
            'class' => 'some-custom-class'
        ]
    ]
]);

If you want to use a different element, for example the error.ctp one, just use the element setting to specify its name

    'flash' => [
        'element' => 'error'
    ]

See also Cookbook > Controllers > Components > Authentication > Configuration Options

Comments