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.
Related
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.
}
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();
}
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.
I found myself stuck or lost in docs!
I'm using Laravel 5.4 and I'm trying to create validation in a controller which requires a request object.
My problem is my route passes parameters, I can't find in the docs an example how you include the Request $request argument and the $id parameter in a Controller method.
Here is my example:
1: SomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
...
public function edit($id) {
$request = Request; // <-- Do I use that instead of passing an arg?
}
2: Routes with a Paramter in -- routes/web.php
Route::match(['GET', 'POST'], '/some/edit/{id}', 'SomeController#edit');
In their example # https://laravel.com/docs/5.4/requests#accessing-the-request they have Request $request this in their controller, but what happens to Route parameters?:
public function store(Request $request) {}
Question: Do I need to edit the route or is the first parameter ignored if its a request?
A: Or would I do this in **SomeController.php?**
public function edit(Request $request, $id)
{
// I would get the $request, but what happens to $id?
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
]);
}
B: Perhaps, something like this?:
public function edit($id)
{
// Or Request::getInstance()?
$this->validate(Request, [
'title' => 'required|unique:posts|max:255',
]);
}
Any advice would help, or even an example!
Doing
public function edit(Request $request, $id)
should match the route you already have. So it's ok to do that. Request $request here is not a required parameter that you pass. Laravel will take care of it.
It works this way because everytime you send a request, well Laravel 'sends' a request for you (GET, POST). So, it always exist. But you can use use it within your controller if you dont pass it to the controller function as a parameter.
However, you can access it through the global helper function request() if you dont want to pass it as a function parameter.
$request = request();
$name = $request->name; // or
$name = request('name');
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.