CakePHP路由 - [home] / slug为URL [英] CakePHP Routing - [home]/slug as URL

查看:115
本文介绍了CakePHP路由 - [home] / slug为URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要映射www .example.com / slug到www.example.com/venues/view/slug,其中slug是特定地点的记录的URL友好名称。



I也想将www.example.com/slug/events映射到www.example.com/events/index/venue:slug。



阅读CakePHP文档后路由,几次,我没有更聪明。我知道如何为每个单独的场地创建这些路线,但我不知道如何让Cake生成路线。

解决方案

您要小心地将某些内容映射到域名后面的第一个路径。这意味着你将打破controller / action / param / param范例。你可以这样做,但这可能意味着你需要在你的路径文件中定义你的网站的每个url,而不是使用Cake的路由魔法。



另一个可能是使用/ v /的场地和/ e /为事件保持你的url短,但分裂的正常范例的url。



如果你还想做你什么请求在这里,你可以做以下类似的事情。

  Router :: connect(
'/: slug',
array(
'controller'=>'venues',
'action'=>'view'
),
array $ b'slug'=>'[a-zA-Z0-9 _-] +'

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

) ;

在您的控制器中,您将访问带有以下内容的slug(使用第一个路由)。

  function view(){
if(isset($ this-> params ['slug'] )){
//在这里用你的代码做一些事情。
}
}


I'm trying to get to grips with Cake's routing, but I'm having no luck finding a solution to my particular problem.

I want to map www.example.com/slug to www.example.com/venues/view/slug, where slug is the URL friendly name of a record for a particular venue.

I also want to then map www.example.com/slug/events to www.example.com/events/index/venue:slug.

After reading the CakePHP documentation on Routing, a few times over, I'm none the wiser. I understand how I would create these routes for each individual venue, but I'm not sure how to get Cake to generate the routes on the fly.

解决方案

You want to be careful mapping something to the first path after the domain name. This means that you would be breaking the controller/action/param/param paradigm. You can do this, but it may mean that you need to define every url for your site in your routes file rather than using Cake's routing magic.

An alternative may be to use /v/ for venues and /e/ for events to keep your url short but break up the url for the normal paradigm.

If you still want to do what you requested here, you could do something like the following. It limits the slug to letters, numbers, dashes, and underscores.

Router::connect(
    '/:slug', 
    array(
        'controller' => 'venues', 
        'action' => 'view'
        ), 
    array(
    'slug' => '[a-zA-Z0-9_-]+'
    )
);
Router::connect(
    '/:slug/:events', 
    array(
        'controller' => 'events', 
        'action' => 'index'
        ), 
    array(
    'slug' => '[a-zA-Z0-9_-]+'
    )
);

In your controller, you would then access the slug with the following (using the first route as an example).

function view(){
    if(isset($this->params['slug'])){
        //Do something with your code here.
    }
}

这篇关于CakePHP路由 - [home] / slug为URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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