Route::get('{name}, [DateController::class, 'holiday']);
and in name variable you get JohnDoe
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a Controller named: DateController which as a Method holiday()
I want to pass an Object to this holiday() method:
Route::get('/',
[
data: {
'name' => 'Mr. Perfectionist'
},
DateController::class, 'holiday'
]
);
I just want to print it like this:
public function holiday($name) {
return $name;
}
Giving Error:
ReflectionException Function () does not exist
Route::get('{name}, [DateController::class, 'holiday']);
and in name variable you get JohnDoe
I know this the way, But I don't want to pass this with Parameter. I want to create data inside this.
Thanks
You can't, just save data to variable;
Route::get('/', [DateController::class, 'holiday']);
public function holiday()
{
$name = 'Mr. Perfecionist';
}
If I use POST then is there any way?
You can use Get or post. Just get the value from the request object
eg $request->name
I still don't understand what is your goal.
Could you read the chapter in the documentation on request. There are examples. And also the chapter covering routing.
Okay, Let me describe in details.
While I will browse / route then a Controller Function suppose holiday() will call. I don't want to pass anything from View. But I need to pass a FIXED name variable to my holiday() function.
After some calculation this function will give me a data. That's not a problem.
Got the point?
Then make a route with that fixed name.
With this you have there a fixed name.
public function holiday()
{
$name = 'Mr. Perfecionist';
}
It doesn't matter if it's GET or POST request.
public function holiday(Request $request) {
return $request->name;
}
I have to pass that name!
Then is not fixed, how you said before it should be.
Do you not understand the difference between request and a parameter being passed in the route.
Site/John
There John is a parameter not request.
Routing parameters are discussed and demonstrated in the routing chapter.
What are you after a parameter or a request object?
Okay.
With Ajax call we can pass data to Controller. But I don't want to take any help of View. Want to pass a variable from Route to Controller.
Thanks
Sorry, but I still don't get it. What does it mean from route to controller?
Please see this example:
Route::view('/',
'welcome',
['name' => 'Taylor']
);
I can return a view directly with a name variable.
Here, without return a view, I want to get just to call a controller function with the name variable.
Now clear to you? For Example:
Route::get('/',
[UserController::class, 'index'],
['name' => 'Taylor']
);
I have tried with this:
public function holiday(Request $request) {
return $request->all();
}
Route:
Route::get('/',
[DateController::class, 'holiday'],
['name' => 'Taylor']
);
It is not giving any Error. But giving me an empty Array!!
And your controller method inside the parentheses get the parameter.
Public function someName ($name = null)
{
echo $name;
@mrperfectionist in this case, you want your User index method to return data rather than a view?
so:
// DateController
public function holiday(Request $request) {
$name = $request->name;
$data = $this->doSomethingWithName($name);
return response()->json($data);
}
Where did you something like that?
This
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
is same as
Route::view('/welcome', [WelcomeController::class, 'index']);
with WelcomeController
public function index()
{
return view('welcome', ['name' => 'Taylor]);
}
So it means you CANNOT do this
Route::get('/',
[DateController::class, 'holiday'],
['name' => 'Taylor']
);
That was an Example to clear my Requirement.
Really bad example.
@mrperfectionist I think it would be better to define the function of the code you are trying to write, rather than the detail.
can you describe what you are building. is it in JavaScript. how are you creating the data. what do you want to do with it when your holiday function runs?
Okay.
Again describing!!
While I will go to this URL ('/') my laravel Application it will show me a Name.
For this purpose, I don't want to load Any view.
Just with my root URL i want pass a Name to a Controller.
While we run a Fresh Laravel Solution, it show me welcome.blade.php file. Because in our Route we declare it to load, Right??
I want to load just a Controller Method with a Name Variable which I want to pass from Route.
Clear??
You cannot pass data into the controller from the routes file.
Create a different route and controller method for each case
Route::get('/bill','holidayBill');
Route::get('/john','holidayJohn');
Route::get('/fred','holidayFred');
Route::get('/paul','holidayPaul');
controller
public function holidayBill()
{
return holiday('MrSmith');
}
public function holidayJohn()
{
return holiday('MrJones');
}
public function holidayFred()
{
return holiday('MrPurple');
}
public function holidayPaul()
{
return holiday('MrBlue');
}
public function holiday($person)
{
}
Okay.
Then forgot about Route.
Is it possible to Call a Controller from index.php file??
For what?
Please or to participate in this conversation.