How do I pass named parameters?
Hello, I am new to CakePHP and trying to learn! Sorry if my question seems simple to answer, but I'm really having a hard time figuring out how to do this!
I have an application that tracks a contract between an appraiser and a client from start to finish. Along with the contract, it tracks all messaging, tasks, and billing info for that contract.
The first step for the user is to create a contract, which involves selecting a client and assigning an employee to handle the contract. Once they have saved the contract, they can begin to add messages or tasks to the contract.
This is where I seem to be running into a problem. I need to be able to have an index page with a list of contracts. When I click on a contract, I want to go to a new page that will have a new menu with links to the messages, tasks and billing pages. When they click on the messages link, then messages associated with the current contract will be listed. They can also click on a link on the messages page that will allow them to post a new message to that contract.
How do I create this "single contract" view with all of its links? And, how do I make all of the links in the menu point to the correct messages? (I am assuming that I would pass a named parameter somewhere, somehow...but I can't seem to find any detailed information on how to work with named parameters that is beginner oriented and bite size!)
Do I need to put my code in my messages controller, or in my contracts controller? Or do I put everything into my messages or contracts model, and then reference it from the controller? As of right now, I have no code whatsoever for this, because I have gotten so frustrated that I have erased everything and am starting over completely from scratch!
Any help you can provide would be greatly appreciated!
Thanks,
Jeremiah
Asked by otisjs01, on 29/12/09
1 Answer
Pretty simple, basically pick a convention and use the params to pass around which contract you want to pull up. The easiest way is to simply use the URL to pass additional parameters.
When you generate a URL like this:
<a href="/contracts/view/1">View this Contract</a>
CakePHP will translate it into something like this (this is in the simple case without any routing in routes.php). This assumes you have mod_rewrite working.
First parameter = name of controller
Second parameter = name of action
Any additional parameters = pass to the action and also store in $this->params
So in this case, I implement in my contracts_controller.php file a method called "view"
public function view($contract_id = null){
// Clean up the data, make sure valid data is passed in
// Look up the contract from my Contract model by id number
}
Alternatively you can do this:
public function view(){
// Make sure this parameter exists and stuff
...
$contract_id = $this->params['pass'][0];
// Look up the contract from my Contract model by id number
}
Now whenever I generate a link like
<a href="/contracts/view/<?php echo $id; ?>">
in any view - it will link to and pass in the contract_id to the controller method.
A level of sophistication up means that instead of just using convention with the third parameter being your contract_id - you "name" the parameter explicitly.
<a href="/contracts/view/contractId:1">View Contract</a>
This causes cakePHP to create an entry in $this->params['named']
$contract_id = $this->params['named']['contractId'];
This provides a really easy way of passing your ids around using URLs.
Here's the page on parameters in the docs
http://book.cakephp.org/view/55/The-Parameters-Attribute-params
Answered by davidwuon 29/12/09
Tagged with
Rating
0
Viewed
1045 times
Last Activity
on 29/12/09