Search code examples
phpcakephpurl-routingfriendly-url

CakePHP friendly seo url


I want to make my url seo friendly. www.example.com/posts/view/1 change for www.example.pl/:slug-:id. Everything works fine, but probably I'm doing something wrong with routing, because when after clicking the urls in paginator, the url is correct, it looks like www.example.pl/:slug-:id , but it appears an error

"The requested address 'www.example.pl/:slug-:id' was not found on this server."

I don't know what's wrong. Here's my code:

Router::connect(
    '/:slug-:id',
    array(
        'controller' => 'posts', 
        'action' => 'view'
    ),
    array(
        'pass' => array('slug' , 'id'),
        'id' => '[0-9]+'
    )
);

in paginator view:

echo $this->Html->link($ad['Post']['title'], array(
    'controller' => 'posts',
    'action' => 'view',
    'slug' => Inflector::slug($post['Post']['title'],'-'),
    'id'=>$post['Post']['id'])
);

I solved the problem.


Solution

  • I solved the problem. In the posts controller my view function was wrong. Here's right correct:

    function view($id = null, $slug = null) {
    $this->Post->id = $this->params['post'];
    $this->set('post', $this->Post->read());