2
votes

In my CakePHP App I connected the following Route:

Router::connect('/:city/dealer/:id', 
    array('controller' => 'dealers', 'action' => 'view'),
    array(
        'pass' => array('city', 'id'),
        'city' => '[a-z]+',
        'id' => '[0-9]+'
        )
    );

This works great and enables: domain.com/washington/dealer/1

But how do I generate the proper HTML link in the View for this URL? If I just do this:

echo $this->Html->link(
    'Testlink',
    array('washington', 'controller' => 'dealers', 'action' => 'view', 1)
);

It adds all the params to the end of the generated link:

http://domain.com/dealers/view/washington/1

How do I do this properly?

2
What happens if you use '/:city/:controller/:id' for your route? Do you want other controllers to use the same pattern? - Lawrence Barsanti
When I use '/:city/:controller/:id', Cake complains about the missing DealerController for domain.com/washington/dealer/1 … There is basically just this controller in the App … - Sebastian
try domain.com/washington/dealers/1 (notice the s on dealer) - Lawrence Barsanti
Plural form works, but that doesn't help with the Link Construction. - Sebastian
Well I kind of got it to work with absolute urls: $this->Html->link(Link, '/washington/dealers/'.$id.''); This seems to be really bad practice, since it all stops working should I change routes. I'd be glad if someone knew a better way. - Sebastian

2 Answers

2
votes

I believe you still need to specify the params, like so:

echo $this->Html->link('Testlink',
    array('controller' => 'dealers', 'action' => 'view', 'city' => 'washington',
                                                         'id'=> 1));

Cake has a similiar example in the cookbook:

<?php
// SomeController.php
public function view($articleId = null, $slug = null) {
    // some code here...
}

// routes.php
Router::connect(
    '/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
    array('controller' => 'blog', 'action' => 'view'),
    array(
        // order matters since this will simply map ":id" to $articleId in your action
        'pass' => array('id', 'slug'),
        'id' => '[0-9]+'
    )
);

// view.ctp
// this will return a link to /blog/3-CakePHP_Rocks
<?php
echo $this->Html->link('CakePHP Rocks', array(
    'controller' => 'blog',
    'action' => 'view',
    'id' => 3,
    'slug' => 'CakePHP_Rocks'
));
0
votes

Hi Sebastian its probably too late to help you, but I may be able to help someone else with this problem. The key to solving your problem is adding to the url method in Helper class. I did this by creating a AppHelper.php in my View/Helper. It looks like this. I changed out my parameter for yours city.

View/Helper/AppHelper.php

<?php
App::uses('Helper', 'View');
class AppHelper extends Helper {

    function url($url = null, $full = false) { 
            if (is_array($url)) { 
                   if (empty($url['city']) && isset($this->params['city'])) { 
                            $url['city'] = $this->params['city']; 
                    }

                    if (empty($url['controller']) && isset($this->params['controller'])) { 
                            $url['controller'] = $this->params['controller']; 
                    }

                    if (empty($url['action']) && isset($this->params['action'])) { 
                            $url['action'] = $this->params['action']; 
                    }
            } 

            return parent::url($url, $full); 
    }

 }
 ?>

Then I create routes like

Router::connect('/:city/dealer/:id', 
array('controller' => 'dealers', 'action' => 'view', 'id'=>':id'),
array('pass' => array('city', 'id'),
      'city' => '[a-z]+',
      'id' => '[0-9]+'
));

Hope this helps :)