0

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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
This isn't maybe a correct syntax but it was something like: $this->Form->create(null, array(url=>array('controller'=>'users','action'=>'login')));
seem like $this -> Form -> create(null, array('url', '/user/register')) works! Thanks!
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
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.