On Sat, Nov 13, 2010 at 3:07 PM, careless_monkey <[email protected]> wrote:
> I'm building a database of solar system for a course.
> Here's what I have problem with:
>
>
> I have a 'regularplanet' model which has 'regularplanets' controller.
> When I go to /htdocs/solar/regularplanets, my page lists all the
> planets (Earth, Venus, etc.). Beside every planet I want to have a
> link 'Attribute' which goes to another page /htdocs/solar/
> regularplanets/attribute which displays the attributes (Mass, Age,
> Revolution time, etc) corresponding to that planet.
>
> Now here is the problem. I have a function Attribute created in the
> Regularplanets controller which has a parameter ($Name). From what I
> understand, when a user clicks on the 'Attribute' beside Earth it
> should pass 'Earth' as the name in the attribute function in the
> controller. But, this is not happening in my program. How do I make
> Cake know that when a link for Earth attribute is clicked, it should
> pass Earth as Name in the controller and should output its attributes.
>
> This is what I have as a link for Attributes in my index.ctp file.
> [code] <?php echo $this->Html->link('Attributes', array('action' =>
> 'Attribute') ); ?>[/code]
>
> Please let me know if someone is still confused about what I'm talking
> about here. I'll try to upload images and explain better.
> Thanks.
>
> This is how my index.ctp looks like:
> [code]
>
> <table>
> <th> Name </th>
>
>
> <?php foreach ($regularplanets as $regularplanet): ?>
> <tr>
>
> <td> <?php echo $regularplanet['Regularplanet']['Name']; ?> </td>
>
> <td> <?php echo $this->Html->link('Attributes', array('action' =>
> 'Attribute') ); ?></td>
>
> </tr>
>
> <?php endforeach; ?>
>
> </table>
>
> [/code]
>
> Also I have this in my controller:
> [code]
> function attribute ($Name){
> $this->Regularplanet->Name = $Name;
> $this->set('regularplanet', $this->Regularplanet->read());
> }
> [/code]
>
This is a pattern normally used with "slugs". You could use the
SluggableBehavior to automatically create a slug, but, in this case,
all of the planets have single-word names so maybe that's unnecessary.
Router::connect(
'/planets/:name',
array(
'controller' => 'planets',
'action' => 'view'
),
'name' => '[a-zA-Z]+',
'pass' => array('name')
);
Be sure to include routes for 'add', 'edit', etc. actions before this
one of the regexp will match on those too.
view (say your data is in variable $d):
echo $this->Html->link(
$d['Planel']['name'],
array(
'controller' => 'planets',
'action' => 'view',
'name' => $d['name']
),
array('title' => 'view planet info')
);
controller:
public function view($name = null)
{
if (!empty($name)
{
$this->redirect(array('action' => 'index'));
}
$this->set(
'data',
$this->Planet->fecthByName($name)
);
}
model:
public function fetchByName($name)
{
return $this->find(
'first',
array(
'conditions' => array(
'Planet.name' => $name
)
)
);
}
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en