Laravel 5 route :: controller路由模型绑定

时间:2021-10-03 21:10:06

I'm creating a new website and have been using route::resource and have loved being able to do a route model binding.

我正在创建一个新的网站,并且一直在使用route :: resource,并且喜欢能够进行路由模型绑定。

Using route::controller though variables show as {one?}/{two?} etc...

使用route :: controller虽然变量显示为{one?} / {two?}等...

Is it possible to use route model binding when using route::controller?

使用route :: controller时是否可以使用路由模型绑定?

I.E.

class UserController extends Controller {
    public function getProfile(User $user)
    {
        return $user->toJson();
    }
}

and

Route::controller('user', 'UserController');

would result in the following route: /user/profile/{user}?

会导致以下路由:/ user / profile / {user}?

1 个解决方案

#1


You can use Route::model() for your Route::controller.

您可以将Route :: model()用于Route :: controller。

But you must bind all params if you need all of them. For example:

但是如果你需要所有参数,你必须绑定所有参数。例如:

Route::model('one','App\User');
Route::model('two','App\User');

Be sure that your uri request is site.com/controller/method_name/param_1/param_n

请确保您的uri请求是site.com/controller/method_name/param_1/param_n

#1


You can use Route::model() for your Route::controller.

您可以将Route :: model()用于Route :: controller。

But you must bind all params if you need all of them. For example:

但是如果你需要所有参数,你必须绑定所有参数。例如:

Route::model('one','App\User');
Route::model('two','App\User');

Be sure that your uri request is site.com/controller/method_name/param_1/param_n

请确保您的uri请求是site.com/controller/method_name/param_1/param_n