@andrewperk Hi Andrew, thank you for your tutorial they're very good! I just have a question about the logout action, I had to allow my users controller for the 'logout' action while you didn't have to ? Any reason why ? Thanks
@andrewperk now it works thanks! but everytime i enter my url i am automatically redirected to /users/login - so i can't access my 'home' page page without beeing logged in
@diepec07 You need to tell the application which actions are allowed to be accessed when not logged in. The video covers this, use $this->Auth->allow('action1', 'action2'); Good luck!
@andrewperk if i put in it array('pages'=>'home') it says that it requires a string not an array but when i only put in ('index','pages=>'home') there is an syntax error
@diepec07 What's the name of your action in your pages controller? When you allow an action using $this->Auth->allow() you don't pass in an array, it's just strings, and it only contains the action name. So if your action in your pages controller is called index, you need to use $this->Auth->allow('index'). If you don't want to allow all index actions for all controllers, you need to put this inside the Pages controller beforeFilter so that it only applies to that index action.
@tyebillion Notice in the video the name of the field in the users table is called 'role' not 'roles'. You need this to match your field name. Make sure you have a role field in your users table and make sure you pas in 'role' and not 'roles' to the $this->Auth->user() method.
@andrewperk Alas that is not the problem as I tried both 'roles' and 'role' fields on the database and matching words in my code. Do you have any other ideas?
@tyebillion Hm, I'm not sure. Try using the controller's set method and pass whatever value $this->Auth->user('role') is giving and send it to your view and echo it out. $this->set('role_value', $this->Auth->user('role')); and in your view echo $role_value to see what it says. Check the user you are logged in as in the database and make sure s/he has a role set in the table. Maybe the role field is empty for that user?
@andrewperk This is really weird the value comes back as empty:
$this->set('test_role', $this->_testRole());
function _testRole() { if ($this->Auth->user()) { if ($this->Auth->user('role') == '') return 'empty'; else return $this->Auth->user('role'); } else return 'nothing';
}
I also noticed that $this->Auth->user('id') is empty too. Do you think there is something wrong with my cake installation? Or something wrong with the initialisation of my Auth component.
@tyebillion Try sending the entire user with $this->Auth->user() to the view and use the debug() method to output and see what's inside. $this->set('user', $this->Auth->user()); in view debug($user); Don't do your if/else checking. Just send it to the view and use debug to view the array.
This is a fantastic video, just what I needed to learn the new Cake 2.0 way of doing things.. I'll be using this as a basis for my next app.
One quick question, however - I'm interested in the idea of having the users login with their email address, rather than a username. How hard would this be to set-up?
@LimeBlastVideo It shouldn't be too hard. Here's some tips. Make an email field in the DB instead of a username field. In your app controller where you initialize the Auth component, add another option with the others for setting the fields: 'fields'=>array('username'=>'email', 'password'=>'password'). This would go inside the Auth array along with your other Auth options for loginRedirect etc.. In your model you can validate the email using the email validation rule and notEmpty
@andrewperk Hi again, thank you for your help. You were almost right, it seems, but there are deeper changes to auth in 2.0.. which, to make a long story short, means that the code to set the fields needs to look like this:
Hi Andrew. Thank you for this and the other wonderful CakePHP tutorials. Although I followed the tutorial exactly I am still faced with a small issue. In my 'Users' controller I have a function called 'signup' and 'confirmation'. I added these two to the 'allow' property in the 'AppController' but now I cannot access anything in the entire application. The browser send this message: " server is redirecting the request for this address in a way that will never complete" and halts. Thanks, Denis
@rendlerdenis Essentially you have an infinite redirecting loop that will never actually redirect anywhere because Auth is denying access to both the original action and the redirect action. Make sure whatever action you redirect to is allowed. It sounds like your login action is not allowed. Try explicitly allowing the login action using the Auth->allow('login')
@andrewperk Hi Andrew. Thank you for your reply. I found the problem using Opera. The issue was that in my main layout I had a call to a 'loadCategories' action which I forgot about and did not set to allow. In FF I could not see the error message, unfortunately and that is why I got stuck. Once I allowed that action everything was working properly. Once again, thank you and if you could make a tutor about installing cake on shared hosting I will greatly appreciate it. Thanks, Denis.
Thanks A LOT for this video! CakePHP really messed my head up before I've found your tutorial and the way you explain things is absolutely stunning since it's clear, precise & absolutely understandable!
i especially like that you explain every step and furthermore you do it in a pleasent and calm manner.. you certainly saved my day!
I have nowhere found such clear, concise, well thought out presentations on any part of the cakePHP framework in the 2+ years I have been mucking about with it. Not only how to code with it but where specific bits of code belong AND what they mean AND why they are relevant.
Is it possible to create multiple admin roles? For example, a manager, and an administrator. Administrators having the same capabilities as Managers, but managers limited in what they can do.
how i can use following if you can help me: i have following column in my table wf_users: wf_username, wf_password now in my form i don´t wanna anyone to se my table structure therefore i use in my Form $this->Form->input("username"); my post looks like this User=>array( 'username', 'password' ); now i need them to change them into my column or that want do the trick to login in the allowed area because it try´s to fetch with wrong column (with "username" not with "wf_usernam").
@webfacer create a WfUsersController.php and a WfUser.php model file. Store views in View/WfUsers/. In your app controller when you define the Auth component add an additional option for setting the 'loginAction'=>array('controller'=>'wfusers', 'action'=>'login'). In your view make sure to set the wf_password field to a password type. Then in your login action pass in the form data array to the $this->Auth->Login($this->request->data) method, like that.
@webfacer Please remember that I do these videos for free and provide quick support for them for free. If you can't make complete sentences and describe your problem properly you aren't going to receive the answer you want. Nor will you get any additional help from me by giving a thumbs down on something simply because I didn't answer your question properly. The solution I gave to you for changing your input names should work, simply rebuild the array after its submitted.
@andrewperk not on the video on your answer i give a thumb down below. the tutorial helped me to find new methods in cakephp sorry but i´m not that goog in english.
could you write the code how do you mean it here bin.cakephp.org. how the the rebuild should look like. sorry for misunderstanding that´s because my motherlanguage is not english
@andrewperk This solution I provided was incorrect. Do not follow this solution. You need to use the authenticate option in your auth component property. Please refer to the cakephp manual you need to set the userModel and the fields which will be used for the username and password. Also do not pass in the data array to the login action. By setting the loginAction and the authentication options you should be able to get it working with another model. I'm sorry for the confusion.
@webfacer It's getting the column username because you called your input username. If you want to keep your input as 'username' and have it submit to the wf_username field you'll probably need to rebuild the data array after it's submitted to the controller so that it calls or saves to the right fields. Auth is tricky and things don't work as expected when changing the defaults.
@andrewperk hmmm thats shit why i can´t use my own variables. because user can see in the input my rows name. if i save i can change it beforeSave why it don´t a method like beforeSend or beforeSendToAuth... to change it there and give the Auth->login() method the right post data for query it in user table
@Lucastin Don't use authenticate. My solution for using a different model/controller/table was to just add a new option to the 'Auth' component config array in your controller 'loginAction'=>array('controller'=>'differentuserscontroller', 'action'=>'login'). Proceed to make the app just as I showed in the video except in your password form field set its type to password and make sure to pass in $this->request->data to the login method in your login action, instead of leaving it blank.
with the $this->request->data, any data inputted into the form will be okay.
And it's not fine for an application.
But without it, i can not log in.
Now I'm trying to following your vid with your files and even if i let the username or password field blank, the page do not shows the error message on setFlash.
@Lucastin You're right, I thought I found a simpler solution than using the authenticate option but it doesn't work properly. You were on the right track for setting the authenticate option. 'authenticate' => array( 'Form' => array( 'userModel'=>'DiffUserModel', 'fields' => array('username' => 'diff_username', 'password'=>'diff_password') )
@Lucastin Check that you're displaying flash messages and auth messages in the layout. The video itself is all correct, I tested it many times both on Linux and on Windows. It's probably just a simple error. If you still don't get it working use pastie.org and paste in your controller code and layout and give me the link to your pasite and send me a message through youtube and I'll try and help you.
Andrew, you are so kind. Thank you. I have been scripting in php for some time now... but the Object Orientation in Cake is pretty different than what I am used to. If I would have stopped a moment and thought about it, I would not have needed to bother you. Thanks again for all the wonderful tutorials and videos.
I have a suggestion/request: I was wondering, would you consider doing a series whereby you integrate something like this login with an existing website, CSS all that? Thx again
Great video, Andrew. I blogged about basic CakePHP 2.0 setup about a month ago, and your video definitely took it to the next level. Your videos are very helpful.
@kazigmal The error message gives you the solution. Your app can't find your logout action. Double check that you made the logout action and that you put it inside the UsersController and not in the AppController. If that doesn't solve your problem use pastie.org and paste in your controller code, hit the paste button and send me the url to your pastie and I'll take a look at it. Good luck.
@andrewperk Hi Andrew, thank you for your tutorial they're very good! I just have a question about the logout action, I had to allow my users controller for the 'logout' action while you didn't have to ? Any reason why ? Thanks
antonyduaut 17 hours ago
Andrew we love your tutorials in Cake 2.0 KEEP EM COMIN!!
pcproff 2 weeks ago in playlist Uploaded videos
Great work, very thorough and clear.
I'd love to see another tutorial like this on the Acl Component
spadict 2 weeks ago
hello andrew!
thanks for this awesome tutorial!
i got one question - i get the error message
Notice (8): Undefined variable: logged_in [APP/View/Layouts/default.ctp, line 64]
do you know why?
diepec07 1 month ago
@diepec07 Check your AppController's beforeFilter and make sure you sent the logged_in variable to your views with the set method.
andrewperk 1 month ago
@andrewperk now it works thanks! but everytime i enter my url i am automatically redirected to /users/login - so i can't access my 'home' page page without beeing logged in
diepec07 4 weeks ago
@diepec07 You need to tell the application which actions are allowed to be accessed when not logged in. The video covers this, use $this->Auth->allow('action1', 'action2'); Good luck!
andrewperk 4 weeks ago
@andrewperk if i put in it array('pages'=>'home') it says that it requires a string not an array but when i only put in ('index','pages=>'home') there is an syntax error
diepec07 4 weeks ago
@diepec07 What's the name of your action in your pages controller? When you allow an action using $this->Auth->allow() you don't pass in an array, it's just strings, and it only contains the action name. So if your action in your pages controller is called index, you need to use $this->Auth->allow('index'). If you don't want to allow all index actions for all controllers, you need to put this inside the Pages controller beforeFilter so that it only applies to that index action.
andrewperk 4 weeks ago
@andrewperk well i dont have an pages controller ;) i use the standard folder app/view/pages ;)
diepec07 2 weeks ago
I have a problem: $this->Auth->user('roles') always returns the empty string for me in Cake 2.
tyebillion 1 month ago
@tyebillion Notice in the video the name of the field in the users table is called 'role' not 'roles'. You need this to match your field name. Make sure you have a role field in your users table and make sure you pas in 'role' and not 'roles' to the $this->Auth->user() method.
andrewperk 1 month ago
@andrewperk Alas that is not the problem as I tried both 'roles' and 'role' fields on the database and matching words in my code. Do you have any other ideas?
tyebillion 1 month ago
@tyebillion Hm, I'm not sure. Try using the controller's set method and pass whatever value $this->Auth->user('role') is giving and send it to your view and echo it out. $this->set('role_value', $this->Auth->user('role')); and in your view echo $role_value to see what it says. Check the user you are logged in as in the database and make sure s/he has a role set in the table. Maybe the role field is empty for that user?
andrewperk 1 month ago
@andrewperk This is really weird the value comes back as empty:
$this->set('test_role', $this->_testRole());
function _testRole() { if ($this->Auth->user()) { if ($this->Auth->user('role') == '') return 'empty'; else return $this->Auth->user('role'); } else return 'nothing';
}
I also noticed that $this->Auth->user('id') is empty too. Do you think there is something wrong with my cake installation? Or something wrong with the initialisation of my Auth component.
tyebillion 1 month ago
@tyebillion Try sending the entire user with $this->Auth->user() to the view and use the debug() method to output and see what's inside. $this->set('user', $this->Auth->user()); in view debug($user); Don't do your if/else checking. Just send it to the view and use debug to view the array.
andrewperk 1 month ago
@andrewperk It just contains two fields: username and password.
tyebillion 1 month ago
This is a fantastic video, just what I needed to learn the new Cake 2.0 way of doing things.. I'll be using this as a basis for my next app.
One quick question, however - I'm interested in the idea of having the users login with their email address, rather than a username. How hard would this be to set-up?
Thank you again :)
LimeBlastVideo 1 month ago
@LimeBlastVideo It shouldn't be too hard. Here's some tips. Make an email field in the DB instead of a username field. In your app controller where you initialize the Auth component, add another option with the others for setting the fields: 'fields'=>array('username'=>'email', 'password'=>'password'). This would go inside the Auth array along with your other Auth options for loginRedirect etc.. In your model you can validate the email using the email validation rule and notEmpty
andrewperk 1 month ago
@andrewperk Hi again, thank you for your help. You were almost right, it seems, but there are deeper changes to auth in 2.0.. which, to make a long story short, means that the code to set the fields needs to look like this:
'authenticate' => array('Form' => array('fields' => array('username' => 'email')))
(as found in the cook book)
Thank you again, again :)
LimeBlastVideo 1 month ago
Hi Andrew. Thank you for this and the other wonderful CakePHP tutorials. Although I followed the tutorial exactly I am still faced with a small issue. In my 'Users' controller I have a function called 'signup' and 'confirmation'. I added these two to the 'allow' property in the 'AppController' but now I cannot access anything in the entire application. The browser send this message: " server is redirecting the request for this address in a way that will never complete" and halts. Thanks, Denis
rendlerdenis 1 month ago
@rendlerdenis Essentially you have an infinite redirecting loop that will never actually redirect anywhere because Auth is denying access to both the original action and the redirect action. Make sure whatever action you redirect to is allowed. It sounds like your login action is not allowed. Try explicitly allowing the login action using the Auth->allow('login')
andrewperk 1 month ago
@andrewperk Hi Andrew. Thank you for your reply. I found the problem using Opera. The issue was that in my main layout I had a call to a 'loadCategories' action which I forgot about and did not set to allow. In FF I could not see the error message, unfortunately and that is why I got stuck. Once I allowed that action everything was working properly. Once again, thank you and if you could make a tutor about installing cake on shared hosting I will greatly appreciate it. Thanks, Denis.
rendlerdenis 1 month ago
Awesome video. You should rewrite CakePHP's documentation, this was considerably easier to understand.
emanuellagos 1 month ago
You saved me again with this video. ty
BlakMetall 1 month ago
public function matchPasswords($data) { return ($data['password'] == $data['password_confirmation']);
}
teckyify 1 month ago in playlist Uploaded videos
I try with cakephp 2.0.3 and do not works. Am I missing something?
From your final sources copy:
-Controllers, changes on AppController and full UsersController
-Moldels, full User.php
-Views, changes on Layouts/Default.php and full /Users (with all views within)
fenix32 2 months ago in playlist Uploaded videos
I run the final code of your tutorial and works great, but when i copy to my project in cakephp 2.0.4, DO NOT LOGIN.
When click in button login (with de correct user/password well created) it redirects to users/index but not login.
Why do you use the full cakephp and not a "cake bake"? (I tried doing it like you but does not work)
fenix32 2 months ago in playlist Uploaded videos
Thanks man! Everything i needed, very well explained! Keep up the good work :)
eddygoe 2 months ago
Thanks A LOT for this video! CakePHP really messed my head up before I've found your tutorial and the way you explain things is absolutely stunning since it's clear, precise & absolutely understandable!
i especially like that you explain every step and furthermore you do it in a pleasent and calm manner.. you certainly saved my day!
kickedINmyHEAD 2 months ago 3
Who IS this Andrew Perkins!!! You are awesome.
I have nowhere found such clear, concise, well thought out presentations on any part of the cakePHP framework in the 2+ years I have been mucking about with it. Not only how to code with it but where specific bits of code belong AND what they mean AND why they are relevant.
Too awesome! Thank you very, very much.
mcscreechca 2 months ago 7
Please my teatcher , i want to some tut about default.ctp and home.ctp
and how to show the data from phpmyadmin into these files ,, ,thx
arabiane199 2 months ago
i've been looking at cakePHP for 24 hours now and holy hell. This is AMAZING!
saves so much time!
robingchann 2 months ago
Great video. Very helpful.
Is it possible to create multiple admin roles? For example, a manager, and an administrator. Administrators having the same capabilities as Managers, but managers limited in what they can do.
NathanLedet 2 months ago
very very helpful video tanks
majortamer 2 months ago
Well done. Thanks for the video.
pmolaughlin 2 months ago
Thanks for putting this together its awesome helped me allot
matthewmcconnonnz 2 months ago
Thanks Andrew, I've been looking everywhere for a good auth tutorial for CakePHP 2.0. This has definitely got me on the right path.
ianhoar 3 months ago
oh cool , love it
ucha19871 3 months ago
Awesome!!!
craigborrows 3 months ago
webfacer 3 months ago
@webfacer create a WfUsersController.php and a WfUser.php model file. Store views in View/WfUsers/. In your app controller when you define the Auth component add an additional option for setting the 'loginAction'=>array('controller'=>'wfusers', 'action'=>'login'). In your view make sure to set the wf_password field to a password type. Then in your login action pass in the form data array to the $this->Auth->Login($this->request->data) method, like that.
andrewperk 3 months ago
@andrewperk thats not what i want and thumb down i do thats why that user can easly find it´s solution like stack overflow
webfacer 3 months ago
@webfacer Please remember that I do these videos for free and provide quick support for them for free. If you can't make complete sentences and describe your problem properly you aren't going to receive the answer you want. Nor will you get any additional help from me by giving a thumbs down on something simply because I didn't answer your question properly. The solution I gave to you for changing your input names should work, simply rebuild the array after its submitted.
andrewperk 3 months ago
Comment removed
webfacer 3 months ago
This has been flagged as spam show
@andrewperk not on the video on your answer i give a thumb down below. the tutorial helped me to find new methods in cakephp sorry but i´m not that goog in english.
could you write the code how do you mean it here bin.cakephp.org. how the the rebuild should look like. sorry for misunderstanding that´s because my motherlanguage is not english
webfacer 3 months ago
@andrewperk This solution I provided was incorrect. Do not follow this solution. You need to use the authenticate option in your auth component property. Please refer to the cakephp manual you need to set the userModel and the fields which will be used for the username and password. Also do not pass in the data array to the login action. By setting the loginAction and the authentication options you should be able to get it working with another model. I'm sorry for the confusion.
andrewperk 3 months ago
@webfacer It's getting the column username because you called your input username. If you want to keep your input as 'username' and have it submit to the wf_username field you'll probably need to rebuild the data array after it's submitted to the controller so that it calls or saves to the right fields. Auth is tricky and things don't work as expected when changing the defaults.
andrewperk 3 months ago
@andrewperk hmmm thats shit why i can´t use my own variables. because user can see in the input my rows name. if i save i can change it beforeSave why it don´t a method like beforeSend or beforeSendToAuth... to change it there and give the Auth->login() method the right post data for query it in user table
webfacer 3 months ago
Nice vid, man!
Thanks a lot.
But I'm trying to use a Model different from USER to control my application, but i'm not getting something from the documentation.
$this->Auth->authenticate ('userModel' => 'Models.Model') seems not work.
Can you post a video about this?
Lucastin 3 months ago
@Lucastin Don't use authenticate. My solution for using a different model/controller/table was to just add a new option to the 'Auth' component config array in your controller 'loginAction'=>array('controller'=>'differentuserscontroller', 'action'=>'login'). Proceed to make the app just as I showed in the video except in your password form field set its type to password and make sure to pass in $this->request->data to the login method in your login action, instead of leaving it blank.
andrewperk 3 months ago
Comment removed
Lucastin 3 months ago
@andrewperk It's not working!
with the $this->request->data, any data inputted into the form will be okay.
And it's not fine for an application.
But without it, i can not log in.
Now I'm trying to following your vid with your files and even if i let the username or password field blank, the page do not shows the error message on setFlash.
I can follow this video until 31:55 min.
Lucastin 3 months ago
@Lucastin You're right, I thought I found a simpler solution than using the authenticate option but it doesn't work properly. You were on the right track for setting the authenticate option. 'authenticate' => array( 'Form' => array( 'userModel'=>'DiffUserModel', 'fields' => array('username' => 'diff_username', 'password'=>'diff_password') )
)
You also still need the loginAction set as well.
andrewperk 3 months ago
@andrewperk thanks, now it's fine!
Lucastin 3 months ago
@Lucastin Check that you're displaying flash messages and auth messages in the layout. The video itself is all correct, I tested it many times both on Linux and on Windows. It's probably just a simple error. If you still don't get it working use pastie.org and paste in your controller code and layout and give me the link to your pasite and send me a message through youtube and I'll try and help you.
andrewperk 3 months ago
Andrew, you are so kind. Thank you. I have been scripting in php for some time now... but the Object Orientation in Cake is pretty different than what I am used to. If I would have stopped a moment and thought about it, I would not have needed to bother you. Thanks again for all the wonderful tutorials and videos.
I have a suggestion/request: I was wondering, would you consider doing a series whereby you integrate something like this login with an existing website, CSS all that? Thx again
kazigmal 3 months ago
Great video, Andrew. I blogged about basic CakePHP 2.0 setup about a month ago, and your video definitely took it to the next level. Your videos are very helpful.
capnhook72 3 months ago
I am following along with your tutorial, but I seem to have missed something...
I am getting the following error when attempting to Logout:
Error: The action logout is not defined in controller UsersController
Error: Create UsersController::logout() in file: app\Controller\UsersController.php.
kazigmal 3 months ago
@kazigmal The error message gives you the solution. Your app can't find your logout action. Double check that you made the logout action and that you put it inside the UsersController and not in the AppController. If that doesn't solve your problem use pastie.org and paste in your controller code, hit the paste button and send me the url to your pastie and I'll take a look at it. Good luck.
andrewperk 3 months ago
Andrew, you rule. Thanks a ton for all of this information... Any chance for a LDAP/Active Directory authentication tutorial?? :D
nyycegy 3 months ago
thanks alot!!!
bluehawk 3 months ago
great, thanx :)
Will you make the tutorial with AclComponent?
sorcy86 3 months ago
Thanks buddy
CamiloSanchez1979 3 months ago