Laravel 4路由,参数数量不受限制 [英] Laravel 4 route with unlimited number of parameters

查看:48
本文介绍了Laravel 4路由,参数数量不受限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为无限数量的URL级别创建动态路由.

I'm trying to create a dynamic route for an unlimited number of URL levels.

这是我当前的路线

Route::get('{pageLink}', array('uses' => 'SiteController@getPage'));

这适用于第一级.因此,像something.com/foo/这样的URL会起作用.但是,如果我有诸如something.com/foo/bar/之类的内容,它将无法捕获该URL.我需要它来匹配无限的水平.这样,在我的控制器中,无论整个链接是什么,它都会为我提供一个变量.

This works for the first level. So a URL like something.com/foo/ would work. But if I had something like something.com/foo/bar/ it wouldn't catch that URL. I need it to match unlimited levels. That way in my controller it'll get me a variable of whatever the entire link is.

我知道我能做

Route::get('{pageLink}', array('uses' => 'SiteController@getPage'));
Route::get('{pageLink}/{pageLink2}', array('uses' => 'SiteController@getPage'));
Route::get('{pageLink}/{pageLink2}/{pageLink3}', array('uses' => 'SiteController@getPage'));

但是这似乎有点过分了.有没有更好的方法可以将其匹配到URL的末尾?

But that just seems like an overkill. Is there a better way to do this so it'll match to the end of the URL?

谢谢.

推荐答案

您可以尝试执行以下操作:

You can try something like this:

//routes.php
Route::get('{pageLink}/{otherLinks?}', 'SiteController@getPage')->where('otherLinks', '(.*)');

请记住,将以上内容放在routes.php文件的最底端(底部),因为它就像一条全部捕获"路由,因此您必须首先定义所有更具体的"路由.

Remember to put the above on the very end (bottom) of routes.php file as it is like a 'catch all' route, so you have to have all the 'more specific' routes defined first.

//controller 
class SiteController extends BaseController {

    public function getPage($pageLink, $otherLinks = null)
    {
        if($otherLinks) 
        {
            $otherLinks = explode('/', $otherLinks);
            //do stuff 
        }
    }

}

此方法应允许您使用无限数量的参数,因此这似乎是您所需要的.

This approach should let you use unlimited amount of params, so this is what you seem to need.

这篇关于Laravel 4路由,参数数量不受限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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