1

My problem: I am currently trying to refactor some of my controllers. Doing so I found these two routes:

Route::get('/events', [EventsController::class, 'eventsList'])->name('event-list'); Route::get('/courses', [EventsController::class, 'allCoursesList'])->name('all-events'); 

they show different filter options in the frontend.

What I want to do:

Example Code:

Route::get('/courses', [ 'all' => 1, EventsController::class, 'courseList' ])->name('all-events'); 

so having the ability to pass a variable, in this case all to my controller. EventsController So I can check with if in my controller and handle these routes differently with only one function instead of two.

With the current solutions on StackOverflow, users are using:

'uses'=>'myController@index' 

now if I try it like this:

Route::get('/courses', [ 'all' => 1, 'uses' => 'EventsController@CourseList' ])->name('all-events'); 

I get the following error:

Target class [EventsController] does not exist. 

Question:
What is the current, correct way, to pass a variable to a controller, from a route. In Laravel 9 and 10.

1
  • Not sure but I think you have to use the FQCN (e.g. \App\Http\Controllers\EventsController@Courselist). Commented Feb 7, 2023 at 12:21

3 Answers 3

4

You can pass arbitrary data to the route as a parameter using the defaults method of Route:

Route::get('courses', [EventsController::class, 'list']) ->name('all-events') ->defaults('all', 1); Route::get('events', [EventsController::class, 'list']) ->name('event-list'); public function list(Request $request, $all = 0) { ... } 

There are also other ways of using the Route to pass data.

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

Comments

1

Laravel versions 8 and above do not automatically apply namespace prefixes. This means that when passing the class name as a string, you need to use the fully qualified class name (FQCN).

For example:

Route::get('/courses', [ 'all' => 1, 'uses' => '\App\Http\Controllers\EventsController@CourseList' ])->name('all-events'); 

If it makes sense for your use case, you could also use URL parameters. For example, if your Course models belong to a Category model, you might do something like this:

Route::get('/courses/{category}', [EventsController::class, 'allCourseList'); 

Then in your countroller, you define the allCoursesList function like so:

public function allCoursesList(Category $category) { // ... do something with $category which is an instance of the Category model. } 

Comments

1

You can use Route Parameters to pass variable from route to controller.

// web.php Route::get('/events/{all}', [EventsController::class, 'eventsList'])->name('event-list'); 

then in your controller you can access the variables

 public function eventsList(Request $request,$all){ if($all==1){ //your logic for handling the condition when $all =1 } // the remaining condition } 

if you have multiple parameters you can pass them like so, you can use ? for optional parameter.

// web.php Route::get('/courses/{param_one}/{param_two?}', [EventsController::class, 'allCoursesList'])->name('all-events'); 

then in your controller you can access the variables

 public function allCoursesList(Request $request,$paramOne,$paramTwo){ return $paramOne.' '.$paramTwo; } 

to access the query parameter

// web.php Route::get('/evets', [EventsController::class, 'allCoursesList'])->name('all-events'); 

if the route were /events?timeframe=0&category=1 you can access the query parameter like so

 public function allCoursesList(Request $request,$paramOne,$paramTwo){ $timeframe= $request->query('timeframe'); // do this for all the query parameters } 

2 Comments

While this could make sense. I dont think that it could work in my case, maybe you have another idea. The URL Parameters are e.g. /events?timeframe=0&category=1
@NiftyMatrix the answer is updated to suite your need

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.