我们是否有机会根据 Laravel 中的参数(获取参数)使用特定的控制器操作?

Is there any chance we can use specific Controller action based on a parameter (get parameter) in Laravel?

基本上,我想要以下路线。

Route::get('/{parameter}', 'SomeController@parameter');

任何帮助都会很棒。提前致谢。

没有内置的方法,但是自己实现起来很简单:

class SomeController {

    public function route($method)
    {
        if ( ! method_exists($this, $method))
        {
            app()->abort(404);
        }

        return $this->{$method}();
    }

}

然后在你的路线中:

Route::get('/{parameter}', 'SomeController@route');

如果您想将任何其他参数传递给它们各自的方法,请使用:

use Illuminate\Http\Request;

class SomeController {

    public function route($method, Request $request)
    {
        if ( ! method_exists($this, $method)) app()->abort(404);

        $parameters = array_slice($request->segments(), 1);

        return call_user_func_array([$this, $method], $parameters);
    }

}