简体   繁体   English

CakePHP友好的seo网址

[英]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 . www.example.com/posts/view/1更改为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 一切正常,但可能我在路由方面做错了,因为在点击paginator中的url后,url是正确的,它看起来像www.example.pl/:slug-:id ,但是它出现了错误

"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: 在paginator视图中:

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. 我解决了这个问题。

Its too simple i'll give you an example from my project .. in your routes.php 它太简单了,我会在你的routes.php中给我一个来自我的项目的例子

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

your link in views should be like . 您在视图中的链接应该是这样的。

$this->Html->link(__('link desu'),array('controller'=>'posts','action'=>'view','id'=>$post['Post']['id'],'slug'=>$post['Post']['slug']));

and your PostsController.php 和你的PostsController.php

public function view($slug,$id){
    $this->Post->id = $id;
    // ....
}

Quick tip : try to create an array in your PostModel to avoid creating it every time in your view . 快速提示:尝试在PostModel中创建一个数组,以避免每次在视图中创建它。 example : 例如:

Post.php post.php中

class Post extends AppModel{
    // ....
        public function afterFind($results,$primary = false){
        foreach ($results as $key => $value) {
            if(isset($value[$this->alias]['id'])){
                $results[$key][$this->alias]['url'] = array(
                                 'controller'=>'posts',
                                 'action'=>'view',
                                 'id'=>$results[$key][$this->alias]['id'],
                                 'slug'=>$results[$key][$this->alias]['slug']
                            );  
            }
            // ....
        }
        return $results;
    }
}

} }

so you can call it in your view simply like that 所以你可以像你那样在你的视图中调用它

$this->Html->link(__('link desu'),$post['Post']['url']);

It's probably a problem with the regex on the route. 这可能是路线上正则表达式的一个问题。 Your slug contain hyphens - which you also use to separate between the slug and the id. 你的slug包含连字符-你也可以用它来分隔slu and和id。 ie: 即:

example.com/my-slug-has-hyphens-1

The regex is not smart enough to know that the "last" hyphen separates the slug from the id. 正则表达式不够聪明,不知道“最后”连字符将slug与id分开。

To test if this is the problem, try using a route like this '/:slug__:id', just to see if it works. 要测试这是否是问题,请尝试使用类似'/:slug__:id',的路由,看它是否有效。

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());

Pass is order sensitive 通行证对订单敏感

In the question the route is as follows: 在问题中路线如下:

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

That means the post function will recieve: 这意味着post函数将收到:

public function view($slug, $id)

As indicated by the edited question, the code is expecting the id to be the first parameter. 如编辑问题所示,代码期望id是第一个参数。 The easiest solution is simply to specify the passed parameters in the order that they are expected: 最简单的解决方案就是按照预期的顺序指定传递的参数:

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

the above code will create correct link as www.example.com/posts/view/title/1 以上代码将创建正确的链接,如www.example.com/posts/view/title/1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM