Created
September 2, 2017 13:12
-
-
Save gabbydgab/df35f753586cd1d3d1374b5d5aeed5a4 to your computer and use it in GitHub Desktop.
Route Model Binding in Laravel Package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Following binding works! | |
*/ | |
namespace MyPackage\Model; | |
class User | |
{ | |
} | |
namespace App\Http\Controller; | |
class UserController | |
{ | |
public function view(User $user) | |
{ | |
dd($user); | |
} | |
} | |
// routes/web.php under App\Http\Controller | |
Route::get(/user/{user}, "UserController@view"); | |
/** | |
* Following binding doesn't works! | |
*/ | |
namespace MyPackage\Model; | |
class User | |
{ | |
} | |
class UserController | |
{ | |
public function view(User $user) | |
{ | |
dd($user); // is null | |
} | |
} | |
// routes/web.php under MyPackage\Http\Controller | |
Route::get(/user/{user}, "UserController@view"); | |
Add the following to your Service Provider files boot method.
// top of file
use Illuminate\Support\Facades\Route;
// inside boot method
Route::group([
'middleware' => ['bindings'],
], function () {
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
});
thank you a lot by clarify the solution.
you solved one of my biggest challenges in making a package in the last few days.
Add the following to your Service Provider files boot method.
// top of file use Illuminate\Support\Facades\Route;
// inside boot method Route::group([ 'middleware' => ['bindings'], ], function () { $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); });
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@monurakkaya I refereed to your https://github.com/monurakkaya/laravel-image src/app/Providers/ServiceProvider.php file and saw the example of the usage that @gabbydgab recommended. Route Model Binding is now working thank you ! ❤️