Skip to content

Instantly share code, notes, and snippets.

@gabbydgab
Created September 2, 2017 13:12
Show Gist options
  • Save gabbydgab/df35f753586cd1d3d1374b5d5aeed5a4 to your computer and use it in GitHub Desktop.
Save gabbydgab/df35f753586cd1d3d1374b5d5aeed5a4 to your computer and use it in GitHub Desktop.
Route Model Binding in Laravel Package
<?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");
@monurakkaya
Copy link

monurakkaya commented Apr 10, 2020

Hey Guys just wondering what your solutions looked like for this. Bit confused on what I need to include and where to write it. When trying to include a wrap i get a RuntimeException: No application encryption key has been specified. Which is strange in my package cause Ive never seen a package with a .env @SadeghPM @monurakkaya
@James-N-M
just run php artisan key:generate

@James-N-M
Copy link

@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 ! ❤️

@James-N-M
Copy link

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');
        });

@MoeinMDN
Copy link

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