user3207586
user3207586

Reputation: 141

How to access url parameters on View::composer

I want to receive the url parameters in the method View::composer but I don't know how to do it...

This is in filters.php:

View::composer('profile.followers', function($view){
    dd($view->parameters);
    $view->with('menus', $menus);
});

This is my route:

Route::get('/outfit/{id}/{slug}', [
    'uses'      => 'PictController@view',
    'as'        => 'pict.view',
    'before'    => 'assets'
]);

This is my Controller:

public function followers($username){
   $d['d'] = Follow::with('followers')->where('id_user_to', 10)->get();
   $this->layout->nest('content', 'profile.followers', $d);
}

Upvotes: 3

Views: 4827

Answers (2)

sanu
sanu

Reputation: 1068

you can do like this

View::composer('profile.followers', function($view){
   //$data contains id/slug
   $viewdata= $view->getData();
   dd($viewdata['id']);

    $view->with('menus', $menus);
});

Upvotes: 0

JofryHS
JofryHS

Reputation: 5874

Probably check out Laravel API doc on Router, you can probably use Route::current() which returns you a Route instance.

Then according to Route documentation, you can use parameters().

For example:

$currentRoute = Route::current();
$params = $currentRoute->parameters();

dd($params); // Will dump the keys and values of current route parameters

UPDATE Note that this only works on 4.1+, prior to this the method names were different.

// 4.1+
Route::current();
Route::parameters();

// Before 4.1
Route::getCurrentRoute();
Route::getParameters();

Upvotes: 4

Related Questions