Laravel 5 setting route parameter from middleware

The problem: I'd like to resolve slugs, if a slug has been used as a route parameter instead of an id.

Attempted Solution: I'm currently getting a parameter from the request in my middleware and trying to set it back to the request. But it seems that this isn't passed to the route (it is passed to subsequent middleware)


Route::get('view_events/{gid}', array('as' => 'view_events','middleware' => ['auth','resolveGroupSlug','groupAdmin'], function($gid) 
    $user = Auth::user();
    $group = Team::find($gid);
    echo $gid;
      //get this user's relationship to group
    $group["team_user"] = Users_team::findComposite($gid,$user["id"]);
    $events = Helpers::getEvents($gid,0);
    return View::make('view_events', array('user' => $user, 'group' => $group, 'events' => $events));

Middleware (resolveGroupSlug):

public function handle($request, Closure $next)
        //convert a string gid to id number
        if (is_string ($request->gid)) {
            $group = Team::where('slug', '=', $request->gid)->firstOrFail();
            $request['gid'] = $group->id;
            echo $request->gid;
        return $next($request);

Any ideas how to set a route parameter in middleware? Or if there is just an easier way of doing this.

(No, i'm not going to copy paste the middleware code into every route i need this in!)



The new value wont get passed as a parameter in the route closure. You need to get it from $request->request->get('gid'); Try dd($gid, $request->request->get('gid')); and compare. Or $request->gid will proxy to the same methodcall

In the end, i didn't use middlewhere, but instead bound a function to routes that have gid as follows

Route::bind('gid', function($gid)
    $group = Team::where('slug', '=', $gid)->orWhere('id', '=', $gid)->first();
    $gid = $group->id;
    return $gid;

This means that all existing code will continue to work, whether people are linking via ids or slugs.

Any feedback, i'd love to know if i'm doing something silly here?

Obviously i'm now aware that i could be passing the group directly to the controller, but some of my controllers are only after the id, not the whole group, so this gives me the best flexibility.

Need Your Help

Swagger API consuming json with json validation

java json rest swagger

I am using Swagger to generate a Restful API:

Error LNK2028 when calling exported C function from C++ wrapper

c++ visual-studio linker-errors dllexport

I have C project from which I export function f() and call it from other C++ project and it works fine. However, when I call some other function g() inside f I get LNK2028 error.