是否可以对控制器路由中的参数重新排序或忽略? [英] Is it possible to reorder or ignore parameters in controller routes?

查看:105
本文介绍了是否可以对控制器路由中的参数重新排序或忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题标题是我能想到的最明确的问题,但是为了清晰起见,这是一个用例/示例:

The question title is the most explicit I could think of, but here's a use case/example for clarity's sake:

说我定义了以下方式来显示文章:

Say I define the following route to show an article:

Route::get('article/{slug}/{id}', 'ArticleController@show');

...

class ArticleController extends BaseController {

    public function show($id)
    {
        return View::make('article')->with('article', Article::find($id));
    }

}

这是行不通的,因为show会将$id参数与$slug参数一起使用.有没有办法只将$id参数传递给show方法?

This won't work, as show will misake the $id parameter with the $slug parameter. Is there a way to pass only the $id parameter to the show method?

推荐答案

可以手动调用控制器函数:

It's possible to manually call controller functions:

Route::get('article/{slug}/{id}', function($slug, $id)
{
    return App::make('ArticleController')->show($id);
});

这篇关于是否可以对控制器路由中的参数重新排序或忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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