I'm trying to create a route in Laravel 5.1 that will search the records base on "keyword". I like to include a ? in my url for more readability. The problem is that when I'm including the ? and test the route with postman it returns nothing. But when I remove the ? and replaced it with / and test it with postman again it will return the value of keyword. Does Laravel route supports ??
//Routes.php
Route::get('/search?keyword={keyword}', [
'as' => 'getAllSearchPublications',
'uses' => 'PublicationController#index'
]);
//Publication Controller
public function index($keyword)
{
return $keyword;
}
I've been searching the internet for hours now and I've read the Laravel documentation, But I can't find the answer. Thank you.
I believe you are talking about query strings. To accept query parameters, you don't pass it as an argument. So, for example, your route should look more plain like this:
Route::get('/search', [
'as' => 'getAllSearchPublications',
'uses' => 'PublicationController#index'
]);
Note: I dropped ?keyword={keyword}.
Then, in your controller method, you can grab the query parameter by calling the query method on your Request object.
public function index(Request $request)
{
return $request->query('keyword');
}
If you didn't already, you will need to import use Illuminate\Http\Request; to use the Request class.
Use
$resquest
Parameter in your controller action to get the query parameter. Instead of using "?" to create in your route.
Related
I started creating a REST API using the lumen framework and wanted to set up a particular behaviour for my GET /user route. Behaviour is the following:
If the request come from an authenticated user (using auth middleware), the method getAllFields from UserController is called and return all the data from the user
If it's not the case, the method get from UserController is called and return some of the data from the user
It seems logic to me to just write it like that in my web.php using a simple middleware:
<?php
$router->group(['middleware' => 'auth'], function () use ($router) {
$router->get('/user/{id}', [
'uses' => 'UserController#getAllFields'
]);
});
$router->get('/user/{id}', [
'uses' => 'UserController#get'
]);
But for some reason, even if the middleware is correct, I always get the response of the second route declaration (that call get()). I precise that if I remove the second route declaration, the one in the middleware work as expected.
Have someone an idea how I can achieve something similar that work?
Router will check if your request matches to any declared route. Middleware will run AFTER that match, so You cannot just return to router and try to find another match.
To fallow Laravel and Routes pattern - You should have single route that will point to method inside controller. Then inside that You can check if user is logged or not and execute getAllFields() from that controller. It will be not much to rewrite since You are currently using UserController in both routes anyway.
web.php
$router->get('/user/{id}', 'UserController#get');
UserController.php
public function get()
{
return auth()->check() ? YourMethodForLogged() : YourMethodForNotLogged();
}
Or if there is not much logic You can keep this in single method.
Also it is good idea to fallow Laravels REST standards (so use show instead of get, "users" instead of "user" etc - read more https://laravel.com/docs/7.x/controllers)
web.php
$router->get('/users/{user}', 'UserController#show');
UserController.php
public function show(User $user)
{
if (auth()->check()) {
//
} else {
//
}
}
To summary - for your needs use Auth inside controller instead of middleware.
To check if user is logged You can use Facade Auth::check() or helper auth()->check(), or opposite Auth::guest() or auth()->guest().
If you are actually using Lumen instead of full Laravel then there is not auth helper by default (You can make own or use package like lumen-helpers) or just keep it simple and use just Facades instead (if You have then enabled in Lumen).
Read more https://laravel.com/docs/7.x/authentication and https://lumen.laravel.com/docs/7.x/authentication
This pattern is against the idea of Laravel's routing. Each route should be defined once.
You can define your route without auth middleware enabled and then define your logic in the controller.
I have this route:
Route::get('/MyModel/{id}', 'MyController#show');
The method show() accepts a parameter called id and I want to setup an alias for /MyModel/1 so it's accesible from /MyCustomURL.
I already tried a few combinations, like:
Route::get('/MyCustomURL', ['uses' => 'MyController#show', 'id' => 1]);
But I keep getting missing required argument error for method show().
Is there a clean way to achieve this in Laravel?
In Laravel 5.4 (or maybe earlier) you can use defaults function in your routes file.
Here is example:
Route::get('/alias', 'MyController#show')->defaults('id', 1);
In this case you don't need to add additional method in your controller.
In same controller (in your case MyController ?) you should create one new method:
public function showAliased()
{
return $this->show(1);
}
and now you can define your aliased route like so:
Route::get('/MyCustomURL', 'MyController#showAliased');
define your route like this:
you can use "as" to give your route any name that you need.
Route::get('/MyModel/{id}' , [
'as'=>'Camilo.model.show',
'uses' => 'MyController#show' ,
]) ;
now if you want to access this route, you can generate url for it, based on its name like this:
route('Camilo.model.show', ['id' =>1]) ;
Route::get('MyModel/{id}', 'MyController#show');
not
Route::get('/MyModel/{id}', 'MyController#show');
Good Luck!
I have a function in Laravel. At the end I want to redirect to another function. How do I do that in Laravel?
I tried something like:
return redirect()->route('listofclubs');
It doesn't work.
The route for "listofclubs" is:
Route::get("listofclubs","Clubs#listofclubs");
If you want to use the route path you need to use the to method:
return redirect()->to('listofclubs');
If you want to use the route method you need to pass a route name, which means you need to add a name to the route definition. So if modify your route to have a name like so:
// The `as` attribute defines the route name
Route::get('listofclubs', ['as' => 'listofclubs', 'uses' => 'Clubs#listofclubs']);
Then you can use:
return redirect()->route('listofclubs');
You can read more about named routes in the Laravel HTTP Routing Documentation and more about redirects in the Redirector class API Documentation where you can see the available methods and what parameters each of them accepts.
Simply name your route:
Route::get('listofclubs',[
'uses' => 'Clubs#listofclubs',
'as' => 'listofclubs'
]);
Then later
return redirect()->route('listofclubs');
One method is to follow the solution provided by others. The other method would be, since you intend to call the function directly then you can use
return redirect()->action('Clubs#listofclubs');
Then in route file
Route::get("listofclubs","Clubs#listofclubs");
Laravel will automatically redirect to /listofclubs.
I'm trying to save a backbone model using model.save().
The url I'm trying to send a POST method to is: http://localhost/user
My route is: Route::POST('/user/{user}', 'Dashboard\Dashboard#newUser');
But I get a Method Not Allowed Exception
Can you see what's wrong in my code?
And you should look at case sensitivity when you look at the documentation.
Route::post('foo/bar', function () {
return 'Hello World';
});
Its lowercase. Sometimes that can cause problems.
The next thing is that "named routes" looks like the following:
Route::get('user/profile', [
'as' => 'profile', 'uses' => 'UserController#showProfile'
]);
I haven't tried that in your way but in that way its working.
And the last thing is that you should pass an ID to your route otherwise the route is not correct. in your case /user/1 for example.
https://laravel.com/docs/5.1/routing
I am using laravel framework. I've setup couple of routes that take a parameter like example.com/route/{dir_name}. When I pass in a dir which has childrens it considers it as another route. Is there a way to bypass it?
I'm using this code:
Route::get('/route/something/{path}',array('as'=>'something',function($path){
return $path;
}));
When I use /route/something/home/user/dev it throws a Symfony\Component\HttpKernel\ Exception\NotFoundHttpException exception.
You can try using constraints on the route parameter with a regular expression.
Route::any('/route/{dir_name}', function ($dir_name) {
return $dir_name;
})->where('dir_name', '.*');
See the docs section about route parameters, specifically the "Regular Expression Route Constraints" part.
You have example.com/route/{dir_name}
I assume your route defined in routes.php
Route::get('/route/{dir_name}', [
'as' => 'someRoute',
'uses' => 'SomeController#index'
]);
In your SomeController.php file you would have this method that takes in the {dir_name}
public function index($dir_name) {
return $dir_name;
}
Instead you have the Route
Route::get('/route/something/{path}',array('as'=>'something',function($path){
return $path;
}));
/route/something/{path}
If you want something in your URL then it has to be in the Route. This is why you're getting the NotFoundHttpException