简体   繁体   English

具有多个参数的路由模型绑定

[英]route model binding with more than one parameter

In route.php, I have this: 在route.php中,我有这个:

Route::get('sells/{sells}/{slug}', 'SellsController@show');

In RouteServiceProvided.php, I have this binding: 在RouteServiceProvided.php中,我具有以下绑定:

$router->bind('sells', function($id, Route $route){
    $action = last(explode('@',$route->getActionName()));
    return ($action == 'edit')? Sell::isActive()->owned()->findOrFail($id) : Sell::isActive()->findOrFail($id);
});

As you see, I have nothing to do with {slug}. 如您所见,我与{slug}没有任何关系。 I want to know how can I pass the slug into the binding closure? 我想知道如何将子弹传递到绑定闭包中?

Any help is appreciated. 任何帮助表示赞赏。

What gets passed to that closure is what it is, as your code isn't calling it, but the framework is. 传递给闭包的是它的实质,因为您的代码没有调用它,但是框架却是。 You could try getting the parameter from the route. 您可以尝试从路线获取参数。 In the closure you have you can pull the parameter: 在闭包中,您可以拉参数:

$route->parameter('slug');

Like this 像这样

 function($id, Route $route) use( $slug ){
     //...........

http://php.net/manual/en/functions.anonymous.php http://php.net/manual/zh/functions.anonymous.php

see example #3 参见示例#3

This lets you pass in additional information at the time you define the closure, ( as opposed to when the closure is executed, with its call arguments ) 这使您可以在定义闭包时传递其他信息(与闭包执行时(带有其调用参数)相反)

If you don't really need to use "bind", use "get" or "post" instead: 如果您确实不需要使用“绑定”,请改用“获取”或“发布”:

$router->get('sells/{sells}/{slug}', function($sell, $slug)
{
    echo 'Sell ID: ' . $sell;
    echo '<br>';
    echo 'Slug: ' . $slug;
});

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

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