Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Deekshith's avatar

Check Parameter exists in all url

I am passing one token to all URL like below,

http://localhost/project/events-manage/123123

here 123123 is the token of a particular branch so same thing s goes with news routes like below,

http://localhost/project/news-manage/123123

Here instead of checking this token exists in every controller function. i can create middle ware right? any suggestions?

0 likes
6 replies
sr57's avatar

Why don't use session variable or cookie?

Deekshith's avatar

I just want to check if that parameter exists in current url. if not present redirect to some other page.

sr57's avatar

Hi @deekshith

So create a route with no parameter that goes where you want to.

tykus's avatar

When you say check if that parameter exists; do you mean a resource having that id, or do you literally mean that the URL has the segment?

Deekshith's avatar

i have pasted example code here,

public function getEventAdd($chapterid)
    {
      if($chapterid)
      {
          $chapterdetail = Chapter::where('id',$chapterid)->first();
          return view('adminfiles.events.addevent',compact('chapterdetail'));
      } else {

          return redirect('admindashboard')->withErrors('Select any one chapter');
      }
    }

if you can see here i am checking chapterid exists or not in every controller function so i am planning to create new middleware to check this route parameter exists or not.

tykus's avatar

If there is no chapterId, then the URL is different; and as a consequence, the Route will not be matched unless you defined the parameter as optional

Please or to participate in this conversation.