Only Bolivian Here
Only Bolivian Here

Reputation: 36733

Generating an html link in CakePHP 2.2.1+?

How can I easily generate an HTML link, using the HtmlHelper class in CakePHP 2.2.1?

Imagine I declared a route that routes /finest-perfumes-ever-2012 to the Perfumes/Index Controller/Action.

I need this generated link to be:

somedomain.com/finest-perfumes-ever-2012  //Generate link HAS to obey Routes I've set.

Instead of:

somedomain.com/Perfumes/Index

The documentation doesn't seem to do much of a good job explaining how to achieve this.

Upvotes: 0

Views: 348

Answers (2)

Arun Jain
Arun Jain

Reputation: 5464

Define route configuration into your app/Config/routes.php at the last of all routing statements.

You can do the same by passing an argument to the action and define it in your routes.php file.

Kindly ask if it not worked for you.

Upvotes: 0

Ross
Ross

Reputation: 17967

Not sure if you have missed out the 2012 by accident or your question is more complicated than my answer below. Assuming the 2012 doesn't matter:

Cake makes use of quite a nifty feature - reverse routing.

If you've set up everything correctly, the following should output what you want.

<?php
Router::connect(
    '/finest-perfumes-ever',
    array('controller' => 'perfumes', 'action' => 'index')
);

echo $this->Html->link('View Finest Perfumes!', array('controller'=>'perfumes', 
                                                         'action' => 'index')); 

Providing your URL (when created using the HTML helper) has parameters that match the route exactly, the reverse routing will look up what you want the route to be, and output links accordingly.

If the 2012 is important you could probably get this working by passing parameters - there are some examples here

Upvotes: 1

Related Questions