Laravel returns a parameter in the class constructor

Here is my route:

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

How can I get the $ companyId argument in __constructor to avoid splitting it in all my actions?

+4
source share
2 answers

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

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

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

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

: , . .

+3

, , , . , , .

Route:: resource. routes.php:

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

, . , /company/ 1:

public function show($company)
{
    // Here you can use, for instance, $company->name
}
0

Source: https://habr.com/ru/post/1542963/


All Articles