让我的Cakephp链接有虚荣网址

I am creating routes for php using a CakeRoute class.

The parse method works fine, but how do I make my urls in the app that are mapped liek:

mydomain.com/teachers/contentProfile/1

to

mydomain.com/chris-willis

I overwrote the parse() method to make my slugs map correctly on a request, but the links don't convert to mydomain.com/chris-willis, they still look the old way.

mydomain.com/teachers/contentProfile/1

Pass the slug instead of the id to the link: array('slug' => $teacher['Teacher']['slug']);

mydomain.com/teachers/contentProfile/

First, make sure you're specifying your URLs with the array syntax; CakePHP cannot translate urls if you provided it a string url.

$this->Html->url(array('controller' => 'teachers', 'action' => 'contentProfile', $id));

instead of

$this->Html->url('/teachers/contentProfile/' . $id);

You may have to alternately provide the slug instead of the ID

$this->Html->url(array('controller' => 'teachers', 'action' => 'contentProfile', 'slug' => 'teacher-name'));

Or, update your custom route class' match() method to lookup the correct slug when only an id is provided.

class CustomRoute {
  match($url) {
    if (empty($url['slug']) && isset($url[0])) {
      // lookup slug
    }
    unset($url[0]);
    return parent::match($url);
  }
}