0

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:

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!)

Thanks!!

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That's true, but would mean that i'd have to have a line of code in every route to replace $gid with $request->gid. I think i may have found a better way though
1

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.

3 Comments

If you need to access this parameter from e.g. middleware: Route::getCurrentRoute()->parameters();
If you try that, you'll get null. As the current route hasn't actually been set yet. Did try that one.
Where you defining those route binds? I've used the route parameter in a middleware before without issues. I define the route binds in App\Providers\RouteProvider

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.