Vra

Here is my route:

Route::controller('/app/{companyId}/', 'HomeController', array('before' => 'auth'));

How can I retrieve $companyId argument in __constructor to avoid retrieving it separate in all my actions?

Was dit nuttig?

Oplossing

If you want to get the parameters in the __construct of your controller you could do this:

class HomeController extends \BaseController
{
    public function __construct()
    {
        $this->routeParamters = Route::current()->parameters();
    }
}

it will return a key value list of parameters for the route (ex: ['companyId' => '1']) @see \Illuminate\Routing\Route

You can also get a specific parameter using the getParameter() or parameter() methods.

NOTE: I'm not sure this is such a great idea tho. There might be a more elegant way to solve or better approach to your problem.

Ander wenke

If you want to make the process simpler, route model binding seems to be the easiest way to go. Instead of having to fetch for the right Model instance in every action of your controller, you pass the right Model to your controller during the routing process.

But you have to use Route::resource. In routes.php :

Route::bind('company', 'Company');
Route::resource('company', 'HomeController');

Then you have an instance of category passed to your controller. For example for /company/1 :

public function show($company)
{
    // Here you can use, for instance, $company->name
}
Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow
scroll top