Take the 2-minute tour ×

I've just started reading up on CakePHP and everything is going pretty good so far, though I have a query on the best way to do the following:

I have a "User" model and a "UsersController" controller.

I currently have a "home" page which is controlled by the home_controller.php (obviously). The home page contains a registration for for a user.

The Question

When the form is posted from the home page, I need to access the User model (from the home controller).

What is the best practice for this task?

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

If I understand the situation correctly, I would post the form to some function in users controller. Then this function would save the data, or log in, or whatever. Finally make redirect back to home for example.

share|improve this answer
Is it possible make a post from the home page to the User controller? as the User controller isn't related to the home page in any way –  GaryDevenay Aug 19 '11 at 20:30
This isn't maybe a correct syntax but it was something like: $this->Form->create(null, array(url=>array('controller'=>'users','action'=>'login'))); –  Henri Aug 19 '11 at 20:34
seem like $this -> Form -> create(null, array('url', '/user/register')) works! Thanks! –  GaryDevenay Aug 19 '11 at 21:16
You should look also Anh Phams answer about correct syntax. It's not good practise to "hard code" the path you want to use as form action. Let's say you want later change the routing for /user/register to for example /signup, but Cake won't automatically route it because of fixed path at form->create. book.cakephp.org/view/948/Defining-Routes –  Henri Aug 20 '11 at 6:52
add comment

you can easily share one model across many controllers

var $uses = array('ModelName');

I do that with User Model and

  • Account Controller (Login, Register, ...)
  • Members Controller (Search, Listing, Profile, ...)
  • Overview Controller (Start Page, Home, ...)

for example. they all share the User Model.

share|improve this answer
add comment

I currently have a "home" page which is controlled by the home_controller.php (obviously) What other methods do you have in home_controller, other than index? And in Cake convention, the controller is plural, so: users, categories... Most likely, you are not aware of pages_controller and routing in Cake.

Anyway, yes, you can make a post to any controller (or even to another domain) from any page, just like any regular HTML page. echo $this->Form->create('User', array('controller'=>'users','action' => 'register')); You can read more here: http://book.cakephp.org/view/1384/Creating-Forms

share|improve this answer
add comment

Your Answer


