简体   繁体   English

从路径将参数传递给控制器

[英]Pass parameter to controller from route

In my routes file, I have; 在我的路线文件中,我有;

Route::get('/{token}/student', [
    'uses' => 'SurveyController@resumeSurvey',
    'as' => 'student',
]);


Route::get('/{token}/city', [
    'uses' => 'SurveyController@resumeSurvey',
    'as' => 'city',
]);

So the route is either "student" or "city". 因此路线是“学生”或“城市”。 How do I determine which one in my controller method? 如何确定控制器方法中的哪一个? Should I even be structuring my routes like this? 我是否应该像这样构造路线? Should I just point them to two different methods? 我是否应该将它们指向两种不同的方法?

I can easily pass in {token} for example with just; 例如,我可以轻松地通过{token}

public function resumeSurvey($token)
{

You should use different methods if you would like different things to do. 如果您想做不同的事情,则应该使用不同的方法。

Example: 例:

Route::get('/{token}/student', [
    'uses' => 'SurveyController@resumeStudent',
    'as' => 'student',
]);


Route::get('/{token}/city', [
    'uses' => 'SurveyController@resumeCity',
    'as' => 'city',
]);

And in your controller you should have two methods: 在控制器中,您应该有两种方法:

public function resumeStudent($token) {

}

public function resumeCity($token) {

}

Then your first route goes to resumeStudent and the ohter route to resumeCity . 然后,您的第一个路线将到达resumeStudent ,而其他路线将到达resumeCity

Inside a controller you can get a current route name by getting a route object Illuminate\\Routing\\Route , at first place, then calling its method getName . 在控制器内部,您可以通过首先获取路径对象Illuminate\\Routing\\Route ,然后调用其方法getName来获取当前路径名称。

The next two ways are the same. 接下来的两种方式是相同的。

public function resumeSurvey($token)
{
    $routeName = Route::getCurrentRoute()->getName();

    $routeName = $this->getRouter()-> getCurrentRoute()->getName());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM