1

i have a middleware, i want it to pass $role to the route.php

public function handle($request, Closure $next)
{
    if ($this->auth->check())
    {

        $role= "normal";
        $user_roles = AssignedRoles::join('roles','role_user.role_id','=','roles.id')
        ->where('user_id', $this->auth->user()->id)->select('roles.is_admin', 'roles.is_vendor')->get();
        foreach($user_roles as $item)
        {
            //var_dump($item->is_vendor);
            //die();
            if($item->is_admin==1)
            {
                $role = "admin";
            }
            if($item->is_vendor==1)
            {
                $role = "vendor";
            }
        }
        if($role=="normal"){
            return $this->response->redirectTo('/');
        }
        //$request->attributes->add(['admin' => $admin, 'vendor' => $vendor]);
        $request->attributes->add(['role' => $role]);
        View::share ('role', $role);

        return $next($request);
    }
    return $this->response->redirectTo('/');
}

is there any way to do that?

My route:

Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function() {
    Route::auth();
    Route::pattern('id', '[0-9]+');
    Route::pattern('id2', '[0-9]+');

    #Admin Dashboard
    Route::get('dashboard', 'Admin\DashboardController@index');

    Route::get('vendor/{id}/edit', 'Admin\VendorController@getEdit');
    Route::post('vendor/{id}/edit', 'Admin\VendorController@postEdit'); 

});
1
  • do you want to use this particular variable in view right ? Commented May 20, 2016 at 5:38

1 Answer 1

1

You can do something like this:

// In your middleware
$request->offsetSet('role', $role);

Then in the routes.php:

use Illuminate\Http\Request;

Route::get('test', ['middleware' => 'auth', function(Request $request) {
    dd($request->get('role')); 
}]);
Sign up to request clarification or add additional context in comments.

4 Comments

i have this error after added Request $request Argument 1 passed to App\Providers\RouteServiceProvider::{closure}() must be an instance of Illuminate\Http\Request, instance of Illuminate\Routing\Router given
this work, Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function($request) {
i can't get the role value using $request->get('role')
@hkguile, don't forget to add the use Illuminate\Http\Request; at the top of your routes.php file, after the <?php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.