{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/12334","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/12334/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/12334/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/12334/events","html_url":"https://github.com/laravel/framework/issues/12334","id":134172356,"node_id":"MDU6SXNzdWUxMzQxNzIzNTY=","number":12334,"title":"[5.3] Explicit route model binding specific to a route group","user":{"login":"hasyimibhar","id":1431045,"node_id":"MDQ6VXNlcjE0MzEwNDU=","avatar_url":"https://avatars0.githubusercontent.com/u/1431045?v=4","gravatar_id":"","url":"https://api.github.com/users/hasyimibhar","html_url":"https://github.com/hasyimibhar","followers_url":"https://api.github.com/users/hasyimibhar/followers","following_url":"https://api.github.com/users/hasyimibhar/following{/other_user}","gists_url":"https://api.github.com/users/hasyimibhar/gists{/gist_id}","starred_url":"https://api.github.com/users/hasyimibhar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hasyimibhar/subscriptions","organizations_url":"https://api.github.com/users/hasyimibhar/orgs","repos_url":"https://api.github.com/users/hasyimibhar/repos","events_url":"https://api.github.com/users/hasyimibhar/events{/privacy}","received_events_url":"https://api.github.com/users/hasyimibhar/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-02-17T04:11:16Z","updated_at":"2018-07-20T09:32:30Z","closed_at":"2016-09-07T08:08:57Z","author_association":"CONTRIBUTOR","body":"#### Summary\n\nCurrently, explicit route model binding is global: once defined, it applies to all routes. It would be nice to be able to define binding specific to a route group. Something like this:\n\n``` php\n$router->group(['namespace' => 'Foo'], function (Router $router) {\n    $router->get('users/{user}', 'FooController@show');\n    // ...\n})->bind('user', function($value) {\n    // ...\n});\n```\n\nOr an alternative syntax:\n\n``` php\n$router->group(['namespace' => 'Foo'], function (Router $router, RouteGroup $group) {\n    $group->bind('user', function($value) {\n        // ...\n    });\n\n    $router->get('users/{user}', 'FooController@show');\n    // ...\n});\n```\n#### Use Case\n\nIn one of my projects, I'm using soft-delete for one of the models. Using implicit model binding, querying for a soft-deleted entity results in 404, which is intended, because to the end-user, the entity is not there. However, at the same time, I want the soft-deleted entities to be visible to the _admins_ (another user of the system.) So for all `/admin/*` routes, querying for a soft-deleted entity should not return 404. Currently, the way I circumvent this issue is by defining a different binding:\n\n``` php\n$router->bind('entity_all', function($value) {\n    // Include soft-deleted entries\n    return SomeEntity::withTrashed()->find($value);\n});\n```\n\nSo when I need to include soft-deleted entries, I use `entity_all` instead:\n\n``` php\n// Route definition\n$router->group(['prefix' => 'admin', 'namespace' => 'Admin'], function (Router $router) {\n    $router->get('entities/{entitiy_all}', 'EntityController@show');\n    // ...\n});\n```\n\n``` php\n// Controller method\npublic function show(SomeEntity $entity_all)\n{\n    // ...\n}\n```\n\nThe drawback with this approach is that I have to use `$entity_all` for the parameter name. It would be nice to be able to still use `$entity` by overriding the model binding for the admin route group:\n\n``` php\n// Route definition\n$router->group(['prefix' => 'admin', 'namespace' => 'Admin'], function (Router $router, RouteGroup $group) {\n    $group->bind('entity', function($value) {\n        // Include soft-deleted entries\n        return SomeEntity::withTrashed()->find($value);\n    });\n\n    $router->get('entities/{entitiy_all}', 'EntityController@show');\n    // ...\n});\n```\n"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/406545245","html_url":"https://github.com/laravel/framework/issues/12334#issuecomment-406545245","issue_url":"https://api.github.com/repos/laravel/framework/issues/12334","id":406545245,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNjU0NTI0NQ==","user":{"login":"stevelacey","id":289531,"node_id":"MDQ6VXNlcjI4OTUzMQ==","avatar_url":"https://avatars0.githubusercontent.com/u/289531?v=4","gravatar_id":"","url":"https://api.github.com/users/stevelacey","html_url":"https://github.com/stevelacey","followers_url":"https://api.github.com/users/stevelacey/followers","following_url":"https://api.github.com/users/stevelacey/following{/other_user}","gists_url":"https://api.github.com/users/stevelacey/gists{/gist_id}","starred_url":"https://api.github.com/users/stevelacey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stevelacey/subscriptions","organizations_url":"https://api.github.com/users/stevelacey/orgs","repos_url":"https://api.github.com/users/stevelacey/repos","events_url":"https://api.github.com/users/stevelacey/events{/privacy}","received_events_url":"https://api.github.com/users/stevelacey/received_events","type":"User","site_admin":false},"created_at":"2018-07-20T09:32:30Z","updated_at":"2018-07-20T09:32:30Z","author_association":"NONE","body":"I don't know what has changed in the 2 years since this issue was opened, but it seems route bindings are still global-only\r\n\r\nI've been using explicit bindings like these so that I can use `uuid` over `api` routes and `slug` over `web` routes:\r\n\r\n```php\r\nRoute::bind('category', function ($value) {\r\n    if (in_array('api', Route::current()->middleware())) {\r\n        return App\\Category::where('uuid', $value)->first() ?? abort(404);\r\n    }\r\n\r\n    return App\\Category::where('slug', $value)->first() ?? abort(404);\r\n});\r\n```\r\n\r\nAlso had some success conditionally limiting the results like this, e.g. allow visibility of all teams over API but only allow the user's own teams via web routes (web being for team management) – works pretty well:\r\n\r\n```php\r\nRoute::bind('team', function ($value) {\r\n    if (in_array('api', Route::current()->middleware())) {\r\n        return App\\Team::where('uuid', $value)->first() ?? abort(404);\r\n    }\r\n\r\n    return request()->user()->teams()->where('slug', $value)->first() ?? abort(404);\r\n});\r\n```"}}
{"actor":{"display_login":"stevelacey"}}