在CakePHP中路由到虚荣的urls [英] Routing in CakePHP to vanity urls

查看:169
本文介绍了在CakePHP中路由到虚荣的urls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个简单和最好的做法在CakePHP(routes.php文件)中将用户ID映射到虚URL的路线?



我在我的路由页面中有(可怕的方法这样做)以下测试代码:

  $ users = array 

1 =>'firstname-lastname',
2 =>'firstname2-lastname2'
);

// profiles
foreach($ users as $ k => $ v)
{
// LESSONS(Profiles)
Router :: connect('/:user',array('controller'=>'teachers','action'=>'contentProfile',$ k),
array('user'=>' :'。$ v。')'));上面的代码路由我的教师控制器与作为操作的conProfile: p>

  mydomain.com/teachers/contentProfile/1 

mydomain.com/firstname-lastname

我可以从路由页面连接到数据库吗?这是不是一个好主意的性能?

解决方案

您可以创建一个自定义路由类来查找传递的url数据库并将其转换为正确的用户ID。设置长缓存时间应该可以缓解对数据库造成的任何性能影响。



但是书的文档有点薄,但基本结构是这样的:

  class TeachersRoute extends CakeRoute {

/ **
*修改传入的参数,以便控制器接收正确的数据
* /
function parse($ url){
$ params = parent :: parse($ url);

//添加/修改参数信息

//教师ID应作为$ params ['pass']数组中的第一个值发送

return $ params;
//或如果查找失败返回false
}

/ **
修改参数,使得调用像HtmlHelper :: url()输出正确的值
* /
function match($ url){
//修改参数

//如果只提供id,则添加$ url ['slug']

return parent :: match($ url);
}

然后在您的路线:

  Router :: connect(
'/:slug',
array(
'controller'=>'teachers',
'action'=>'contentProfile'
),
array(
'slug'=>'[a-zA-Z0-9 _-] +'
'routeClass'=>'TeachersRoute',

);


I was wondering if there was an easy and best practices way to make routes in CakePHP (routes.php file) to map userIDs to a vanity url?

I have (terrible way to do this) the following test code in my routes page:

$users = array
(
    1 => 'firstname-lastname',
    2 => 'firstname2-lastname2'
);   

//profiles
foreach($users as $k => $v)
{
    // LESSONS (Profiles)
    Router::connect('/:user', array('controller' => 'teachers', 'action' => 'contentProfile', $k),
        array('user' => '(?i:'.$v.')'));
}

The above code routes my teachers controller with conProfile as the action from:

mydomain.com/teachers/contentProfile/1
to
mydomain.com/firstname-lastname

Can I connect to the db from the routing page? Is that not a good idea in terms of performance? Let me know what's the best way to do this.

解决方案

You can create a custom route class that will look up passed urls in the database and translate them to the correct user id. Setting a long cache time should mitigate any performance impact of hitting the DB.

The book documentation is a little thin, however, but the basic structure is this:

class TeachersRoute extends CakeRoute {

  /**
   * Modify incoming parameters so that controller receives the correct data
   */
  function parse($url) {
    $params = parent::parse($url);

    // Add / modify parameter information

    // The teacher id should be sent as the first value in the $params['pass'] array

    return $params;
    // Or return false if lookup failed
  }

  /**
   * Modify parameters so calls like HtmlHelper::url() output the correct value
   */
  function match($url) {
    // modify parameters

    // add $url['slug'] if only id provided

    return parent::match($url);
  }

And then in your routes:

Router::connect(
  '/:slug', 
  array(
    'controller' => 'teachers', 
    'action' => 'contentProfile'
  ), 
  array(
    'slug' => '[a-zA-Z0-9_-]+'
    'routeClass' => 'TeachersRoute',
  )
);

这篇关于在CakePHP中路由到虚荣的urls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆