How to get Route URL param in Controller | Laravel - php

I have the following route:
Route::get('/category/{category}/keyword/{keyword}', 'CategoryController#search');
In my controller I am trying to retrieve both the URL params using the following code:
public function search(Request $request)
{
$request->all();
...
}
The above code doesn't return the value of parameters.
If I call the following code, I get the value:
$request->category
Can someone tell me what am I doing wrong?
Thanks!

Try this:
public function search($category, $keyword)
or this:
public function search(Request $request, $category, $keyword)
If you need the Request object.
The route parameters are injected in the funcion call, they are not in the request inputs.

Related

Laravel any route with optional GET parameter

I am using Laravel 8.1. Currently I have this catch-all route:
Route::any('/{any}', [IndexController::class, 'index'])->where('any', '.*');
This works great for all URLs. However if I want to attach a GET parameter for ID, I have do this:
/api/products/view?id=1
Instead of that I want to do this:
/api/products/view/1
My function signature is like this:
public function index(Request $request) {}
I only want ID as an optional GET parameter.
You can add another optional route parameter and specify a pattern in the where for the any parameter - ".*" will not work with optional parameter after any
Route::any('/{any}/{id?}', [IndexController::class, 'index'])->where('any', '[A-Za-z-_]+');
public function index(Request $request)
{
$id = $request->route('id');
}
You can add another route param and get it in the Controller like I have below:
Route:
Route::any('/{any}/{id}', [IndexController::class, 'index'])->where('any', '.*');
Controller:
public function index($any, $id)
{
// $id will be passed in.
}

How can i get url parameter in laravel?

This is my url -> http://localhost:82/?search=asd
How do I catch 'asd' in route?
I try this -> Route::get('/?search={SearchValue}', 'TryController#search');
But it didn't work.
It won't even get into the controller.
You don't have to adjust the routes. But include $request in your controller method. Then use your request object to access it.
use Illuminate\Http\Request;
public function search(Request $request) {
// to access the query parameters
$search = $request->query->get('search');
// similar but different syntax
$search = $request->query('search');
// generic method that checks all input including query
$search = $request->input('search');
}
You should just only need something like this:
Route::get('/search', 'TryController#search')->name('try.search');
Once you have the route correctly set you can call:
public function search(Request $request)
{
$request->get('search')
to get the url parameter you passed into the request.

How call specific function dynamically that we get in url/path in laravel

My Code in route.php:-
Route::get('/register/{id}',array('uses'=>'UserRegistration#id));
I want to call function id (that can be any function of controller) in UserRegistration controller.
Url is like this:- http://localhost:8000/register/test,
http://localhost:8000/register/login
here test and login are function in controller.
{id} is the parameter you're passing to route. So, for your routes go with something like this:
Route::get('/register/id/{id}',array('uses'=>'UserRegistration#id));
//this route requires an id parameter
Route::get('/register/test',['uses'=>'UserRegistration#test]);
Doing this you can call your functions but it is not the recomended way of doing it. Having separete routes foreach function allows for a more in depth control.
public function id(Request $request)
{
return $this->{$request->id}($request);
}
public function test(Request $request)
{
return $request->all();
}

Route to controller issues

I'm currently studying laravel and for my small project, and I'm having a small issues now.
I'm trying to handle php input in url i.e http://example.com/?page=post&id=1
I currently have this in my controller for post.blade.php
public function post($Request $request)
{
$page = $request->input('page');
$id_rilisan = $request->input('id');
$post = Rilisan::where('id_rilisan', '=', $id_rilisan)->first();
if($post = null)
{
return view('errors.404');
}
return view('html.post')
->with('post', $post);
}
and this is the controller
Route::get('/', 'TestController#index');
Route::get('/{query}', 'TestController#post' );
How to process the php input to be routed to controller? I'm very confused right now, I've tried several other method for the Route::get
This route Route::get('/', 'TestController#index') directs user to the index route. So, if you can't change URL structure and you must use this structure, you should get URL parameters in the index route like this:
public function index()
{
$page = request('page');
$id = request('id');
You can use it a as parameter on your controller :-) see this answer please: https://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller
for example query parameter in route would be $query parameter in controller method :-)
So like this:
Route::get('/{query}', 'TestController#post' );
//controller function
public function controllerfunc($query){}
Why do you need to use query parameters in url. You can simply use this structure http://example.com/posts/1
Then your routes will look like this:
Route::get('/posts/{post}', 'PostsController#show');
And you will be able to access Post model instantly in your show method.
Example:
public function show(Post $post) {
return view('html.post', compact('post'));
}
Look how small is your code now.

Accessing Lumen/Laravel URL params

I guess I'm missing something really obvious here but how do you access URL params in Lumen? I have the following route:
$app->get('user/{id}', ['uses' => 'userController#testId']);
Then in my user controller I have:
public function testId(Request $request) {
return $request->input('id');
}
But the ID is always null what have I missed even in this very basic example?
Okay, doesn't matter, as I thought I'm the tool here.
For anyone else (and myself in the future when I land on this page after a Google with the exact same problem next year) the documentation states:
If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies. For example, if your route is defined like so:
$app->put('user/{id}', 'UserController#update');
public function update(Request $request, $id)
{
//
}
So you need to pass any URL params into the function, they don't appear to be accessible via Lumens $request object.
public function testId(Request $request, $id) {
return $id;
}
According to https://laravel.com/docs/master/requests you can do the following:
$app->get('user/{id}', ['uses' => 'userController#testId']);
public function testId(Request $request, $id) {
//$id is from the path
}
The reasoning is that id is part of the request path and not the request input
Try with this
public function testId(Request $request) {
return $request->id;
}

Categories