laravel controller function parameters - php

I'm trying to call a function inside one of my controller from the action() helper function. I need to pass a paramter to my function.
Here is the funciton I'm trying to call :
public function my_function($clef = 'title')
{
$songs = Song::orderBy($clef)->get();
return View::make('my_view', compact('songs'));
}
Here is the way I call it :
Author
The function is always running with the default value, even if I put anything else in my call. From what I can see in my address bar, the paramter seems to be sent along with the call :
http://www.example.com/my_view?clef=author
From the little I know, it seems correct to me, but since it doesn't work, I must come to the evidence that it isn't. What would be the cleanest way to call my function with the right parameter?

The reason why it's not working is because query strings aren't passed as arguments to your controller method. Instead, you need to grab them from the request like this:
public function my_function(Request $request)
{
$songs = Song::orderBy($request->query('clef'))->get();
return View::make('my_view', compact('songs'));
}
Extra tidbit: Because Laravel uses magic methods, you can actually grab the query parameter by just doing $request->clef.

Laravel URL Parameters
I think assigning parameters need not be in key value pair. I got it to work without names.
If your route is like /post/{param} you can pass parameter as shown below. Your URL will replace as /post/100
URL::action('PostsController#show', ['100'])
For multiple parameters say /post/{param1}/attachment/{param2} parameter can be passed as shown below. Similarly your url will be replaced as /post/100/attachment/10
URL::action('PostsController#show', ['100', '10'])
Here show is a method in PostsController
In PostsController
public function show($param1 = false, $param2 = false)
{
$returnData = Post::where(['column1' => $param1, 'column2' => $param2 ])->get();
return View::make('posts.show', compact('returnData'));
}
In View
Read More
In Routes
Route::get('/post/{param1}/attachment/{param2}', [ 'as' => 'show', 'uses' => 'PostsController#show' ] );
URL Should be: http://www.example.com/post/100/attachment/10
Hope this is helpful.

Related

Laravel 9 pass variable from route to controller

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.
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.
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.
}
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
}

How can I add function name as route in Laravel 5.7?

I have a controller which returns enums for respective fields. e.g.
// Expected route - /api/getFamilyTypes - only GET method is allowed
public function getFamilyTypes()
{
return [
'Nuclear Family',
'Joint Family'
];
}
I've around 20 functions like this. How can I add this without manually adding an entry per function in routes file?
Thanks in advance.
In your routes file, add something like this,
Route::get('/something/{func}', 'SomeController#functionRoute');
Where something is whatever path you're wanting to use and SomeController is the controller with the 20 functions you're using and functionRoute is the action that we're about to make.
Then in your controller, make a function like this,
public function functionRoute($func)
{
return $this->$func();
}
This will make it so that whenever someone browses to /something/* on your website, it'll execute the function name at the end. So if you navigate to /something/getFamilyTypes it'll run your getFamilyTypes function.
This isn't particularly secure. If you do this, the user will be able to run any of the controller's methods. You could set up a blacklist like this.
public function functionRoute($func)
{
$blacklist = [
'secret',
'stuff',
];
return in_array($func, $blacklist) ? redirect('/') : $this->$func();
}
Or you could set up a whitelist like this,
public function functionRoute($func)
{
$whitelist = [
'getFamilyTypes',
'otherUserFriendlyStuff',
];
return in_array($func, $whitelist) ? $this->$func() : redirect('/');
}
If the responses are always from hard-coded arrays (as opposed to being from a database) then one way might be to have a variable in your route:
Route::get('/api/enum/{field}', 'EnumController#getField');
And then in your controller method, use the variable to get the correct data from a keyed array:
public function getField($field)
{
$fields = [
'family' => [
'Nuclear Family',
'Joint Family'
],
// ...
];
return $fields[$field];
}
If you want to continue using different methods for every field then Michael's answer is the easiest option, with one caveat. Allowing users to call any method by name on your controller is a security risk. To protect yourself, you should validate the method name against a whitelist.

get parameter without define in function get from route in laravel

public function login($companyId="",$invoiceId="")
{
}
$companyId I want to fetch from URL get its working fine for me but here I want to get $companyid as input::get
public function login()
{
$companyid = Input::get();
}
I am getting null becourse i cannot pass name in get() becouse i m fetching it from url so how can i achive it ........
value i have passed from route
First you need to look at your route definition. If your route is as
Route::get('/login/{companyId?}/{invoiceId?}', array('as'=> 'login', function($companyId="",$invoiceId="") {}));
then remove those parameters and change it to
Route::get('/login', array('as'=>'login',function(){....}));
After that you can change your route call to
route('login', array('companyId'=> 1231323, 'invoiceId'=> 123123));
this arguments you passed in array will be query string. It is important to remove those dynamic segments from your Route definition.

get passed ID from an action

In my blade I tried to pass the ID in an action link.
test
I get the passed id like this in my controller:
public function edit()
{
$input = Input::get();
dd($input);
}
but the output of dd() is:
array:1 [▼
5 => ""
]
But why is it an array? I only want the number but I don't see why laravel gives me the input as an array.
Input::get() gives you the whole input array. Use the specific key to get a specific value, for instance Input::get('id'). But your action() call appears to just append the id without a key. Try e.g. action('DomainController#edit', ['id' => $domain->id]).
That said, you may want to look at named routes and route model binding - makes the whole thing easier.
I think that You should pass $id as a parameter in edit:
public function edit($id)
{
dd($id);
}

laravel route with parameter not coming from url

I have multiple routes that look like this:
Route::get('pending-submit', 'CasesController#cases');
Route::get('submited', 'CasesController#cases');
Route::get('closed', 'CasesController#cases');
I have been looking around even in the router API documentation and I can't find a solution for my requirement other than creating multiple methods within the controller. The method does the exact same query except for adding a where clause to identify the different status between each case, what I was trying to do is have a method like this
public function cases($whereStatus = 0){
return Cases::where('status', $whereStatus)->get();
}
Instead of doing this:
public function pendingCases(){
return Cases::where('status', 0)->get();
}
public function submitedCases(){
return Cases::where('status', 1)->get();
}
public function closedCases(){
return Cases::where('status', 2)->get();
}
But I can figure a way to pass that parameter to the method from the route so I now have to create a method for each route which does not seem necessary to me. I understand I could just generate urls with the get parameter in it but I wanted to make that cleaner, is there a way for me to add that parameter without having it in the url?
By the way, I also tried something like this which did not wok:
Route::get(
'pending-submit',
array(
'uses' => 'CasesController#cases',
'params' => array(
'filter' => 0
)
)
);
EDIT:
I understand I can make URLs like https://someurl.com/cases?status=0 and can also have URLs like https://someurl.com/cases which require a different method per route however what I want is have URLs like https://someurl.com/cases and have a single method where the parameter is passed by the router instead of me getting it from the request so I can do it like this:
public function myMethod($param){
/*
* Here I access the $param without calling Request::input('someparam');
* or without Request::path() where then I have to check what path is it
*/
echo $param; /* this should already have the param from the route */
}
EDIT:
#AndyNoelker what I have is 3 different values either 0, 1 or 2
I want to have something like this
Route::get(
'cases',
array(
'uses' => 'CasesController#cases',
'status' => 0 /* this is what I need */
)
);
If not possible from the routes.php it is fine, I just want to know, all other methods you are giving me is not what I want or asking for since I already know how to do those.
You are going to have to pass the desired status in through the URL - otherwise the route will have no way of knowing which status you desire. You can either do it through URL query parameters or as a fully-fledged route parameter. I would personally suggest using a query parameter in this case, but I'll show you both.
Using Query parameters
URL
example.com/cases?status=1
Routes
Route::get('cases', CasesController#cases);
CasesController
public method cases(Request $request)
{
$input = $request->all();
$status = $input['status'];
return Cases::where('status',$status)->get();
}
Using Route parameters
URL
example.com/cases/1
Routes
Route::get('cases/{id}', CasesController#cases);
CasesController
public method cases($id)
{
return Cases::where('status',$id)->get();
}
Of course if you'd prefer that they use a slug or something other than a unique id in the route, then you'd have to adjust for that in your query, but this should give you the right idea.
I think you're looking for route parameters:
Route::get("/cases/{case}", "CasesController#cases");
This will match any of the following:
some_url/cases/pending-submit
some_url/cases/submited
some_url/cases/closed
...
Then, your function public function cases in CasesController would look like this:
public function cases($case){
if($case == "pending-submit"){
// Do Something
} else if($case == "submited") {
// Do Something Else
}
// OR
return Cases::where('status', $case)->get();
}
Etc etc. Look more into url parameters here: Documentation
Route::get('pending-submit', array('status' => 0, 'uses' => function(){
$CasesController = $app->make('CasesController');
return $CasesController->callAction('cases', $parameters = array());
}));
If I am understanding your question correctly, this should work.
Web Route
Route::get('pending-submit', [
'as' => 'pending',
'uses' => 'CasesController#cases',
'status-id' => '0'
]);
And, you can access the parameter passed with the route like below,
In Controller
$request->route()->getActions('status-id')
In View
{{ request()->route()->getAction('status-id') }}
Tested and perfectly works in L5.6

Categories