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!
Related
I have the followning method in my controller:
public function showQualityResult($qualityData) {
return $qualityData;
}
When clicked on a link , i want that method to be invoked , so i have the following in my view file:
Submited Quality Check
Also, I have the following route setup:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult');
But having the below line of code:
Submited Quality Check
Does't really work , i get the following error in the frontEnd:
Now how can i solve this problem , and why am i getting this error of Route not defined , even though i have the route defined ??
route() helper uses route name to build URL, so you need to use it like this:
route('quality-result.show', session('quality-data'));
And set a name for the route:
Route::get('/showQualityResult', ['as' => 'quality-result.show', 'uses' => 'QualityCheckController#showQualityResult']);
Or:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')->name('quality-result.show');
The route function generates a URL for the given named route
https://laravel.com/docs/5.3/routing#named-routes
If you don't want to use route names, use url() instead of route()
The route() helper looks for a named route, not the path.
https://laravel.com/docs/5.3/routing#named-routes
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')
->name('showQualityResult');
Should fix the issue for you.
In my case I had simply made a stupid mistake.
Further down in my code I had another named route (properly named with a unique name) but an identical path to the one Laravel told me it couldn't find.
A quick update to the path fixed it.
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've been reading everywhere but couldn't find a way to redirect and include parameters in the redirection.
This method is for flash messages only so I can't use this.
return redirect('user/login')->with('message', 'Login Failed');
This method is only for routes with aliases my routes.php doesn't currently use an alias.
return redirect()->route('profile', [1]);
Question 1
Is there a way to use the path without defining the route aliases?
return redirect('schools/edit', compact($id));
When I use this approach I get this error
InvalidArgumentException with message 'The HTTP status code "0" is not valid.'
I have this under my routes:
Route::get('schools/edit/{id}', 'SchoolController#edit');
Edit
Based on the documentation the 2nd parameter is used for http status code which is why I'm getting the error above. I thought it worked like the URL facade wherein URL::to('schools/edit', [$school->id]) works fine.
Question 2
What is the best way to approach this (without using route aliases)? Should I redirect to Controller action instead? Personally I don't like this approach seems too long for me.
I also don't like using aliases because I've already used paths in my entire application and I'm concerned it might affect the existing paths if I add an alias? No?
redirect("schools/edit/$id");
or (if you prefer)
redirect("schools/edit/{$id}");
Just build the path needed.
'Naming' routes isn't going to change any URI's. It will allow you to internally reference a route via its name as opposed to having to use paths everywhere.
Did you watch the class Illuminate\Routing\Redirector?
You can use:
public function route($route, $parameters = [], $status = 302, $headers = [])
It depends on the route you created. If you create in your app\Http\Routes.php like this:
get('schools/edit/{id}', 'SchoolController#edit');
then you can create the route by:
redirect()->action('SchoolController#edit', compact('id'));
If you want to use the route() method you need to name your route:
get('schools/edit/{id}', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
// based on CRUD it would be:
get('schools/{id}/edit', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
This is pretty basic.
PS. If your schools controller is a resource (CRUD) based you can create a resource() and it will create the basic routes:
Route::resource('schools', 'SchoolController');
// or
$router->resource('schools', 'SchoolController');
PS. Don't forget to watch in artisan the routes you created
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.
I would like to know how to put a parameter in a Laravel controller method not obligatory, as I've got a route like this (I'm using the ? to say it's not obligatory):
Route::get('/account/student/create/{student_id?}', array(
'as' => 'account-student-create',
'uses' => 'StudentController#getCreate'
));
I've got in my controller:
getCreate($student_id){...}
Put if don't fill a parameter in the route I've got a warning message:
Missing argument 1 for StudentController::getCreate()
Thanks for helping!
Jean
Add a default value to your controller method declaration:
getCreate($student_id=null){...}
That way when the method is called without a value, $student_id will be set to null.
you could write your function with a default value
public function getCreate($student_id = 'defaultValue')
{
...
}