I would like to define {id} an optional parameter in the following route:
Route::get('profile/edit/{id}', array('uses' => 'BusinessController#editProfile', 'as' => 'profile.edit'));
How can I do this and also define a default parameter if one isn't provided?
Just like the other answers, but as for the default part: I asked almost the same question a few days ago and the answer is:
Route::get('profile/edit/{id?}', array('uses' => 'BusinessController#editProfile', 'as' => 'profile.edit'))
->defaults('id', 'defaultValue');
The important things are
The questionmark
The defaults function
Route::get('profile/edit/{id?}', array('uses' => 'BusinessController#editProfile', 'as' => 'profile.edit'));
you can pass {id?} for optional parameters in your route.
Laravel will take it as a optional. it is called as a wild card in laravel
Just put a question mark after it in the route, and give it a default in the function:
Route::get('profile/edit/{id?}', ...
public function editProfile ($id = 123) { ... }
Documentation: https://laravel.com/docs/5.4/routing#parameters-optional-parameters
Related
In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});
I've been trying to find some documentation on how to accomplish the following, but it seems like maybe I'm not using the correct search terms.
I would like to implement some simplified routes in Laravel 5.4 by omitting the route name from the path – for example:
/{page} instead of /pages/{page}
/profile instead of /users/{user}/edit
/{exam}/{question} (or even /exams/{exam}/{question}) instead of /exams/{exam}/questions/{question}
Example of current routes
Route::resource('exams.questions', 'ExamQuestionController', ['only' => ['show']]);
// exams/{exam}/question/{question}
I know how to do this with route closures and one-off routes (e.g.: Route::get...) but is there a way to do this using Route::resource?
In rails the above could be accomplished with:
resources :exams, path: '', only: [:index, :show] do
resources :question, path: '', only: [:show]
end
// /:exam_id/:id
While I haven't yet found a way to accomplish my test cases using strictly Route::resource, here is what I implemented to accomplish what I was trying to do:
// For: `/{exam}/{question}`
Route::group(['as' => 'exams.', 'prefix' => '{exam}'], function() {
Route::get('{question}', [
'as' => 'question.show',
'uses' => 'QuestionController#show'
]);
});
// For: `/exams/{exam}/{question}`
Route::group(['as' => 'exams.', 'prefix' => 'exams/{exam}'], function() {
Route::get('{question}', [
'as' => 'question.show',
'uses' => 'QuestionController#show'
]);
});
// For: `/profile`
Route::get('profile', function() {
$controller = resolve('App\Http\Controllers\UserController');
return $controller->callAction('edit', $user = [ Auth::user() ]);
})->middleware('auth')->name('users.edit');
// For: `/{page}`
// --------------
// Note that the above `/profile` route must come before
// this route if using both methods as this route
// will capture `/profile` as a `{page}` otherwise
Route::get('{page}', [
'as' => 'page.show',
'uses' => 'PageController#show'
]);
No, you cannot and should not be trying to do this with Route::resource.
The whole purpose of Route::resource is that it creates the routes in a specific way that matches the common "RESTful Routing" pattern.
There is nothing wrong with wanting simpler routes (no one is forcing you to use RESTful routing), but you will need to make them yourself with Route::get, etc. as you already know.
From the documentation (not exactly your case, but related to it - showing that Route::resource is not meant to be super-configurable):
Supplementing Resource Controllers
If you need to add additional routes to a resource controller beyond the default set of resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes:
Route::get('photos/popular', 'PhotoController#method');
Route::resource('photos', 'PhotoController');
I want to pass a parameter from my route to a controller method, to avoid duplication of code. For example, I could have my routes like this
Route::get('used-cars', array('uses' =>
'CarController#indexUsed'));
Route::get('new-cars', array('uses' =>
'CarController#indexNew'));
Route::get('new-and-used-cars', array('uses' =>
'CarController#indexNewAndUsed'));
and then have specific code for in each method for retrieving that car type. However, I would like to just have one index method in the controller, with a variable passed to it to indicate if it is a new or used car.
For example:
Route::get('used-cars', array('uses' =>
'CarController#index(1)'));
Route::get('new-cars', array('uses' =>
'CarController#index(2)'));
Route::get('new-and-used-cars', array('uses' =>
'CarController#index(3)'));
In previous versions of Laravel I believe this could be achieved using something like this
Route::get('/used-cars', array('as' => 'used-cars', function(){
return App::make('CarsController')->index(1);
}));
However I understand this was removed in Laravel 5.4. When I try it I only get class not found for the controller.
You could use a named parameter (type), and restrict that to specific values (new, used).
Route::get('{type}-cars', ['as' => 'cars.new-used', 'uses' => 'CarController#index'])->where('type', 'new|used');
Here are my two routes mentioned in http/routes.php
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{projname}/{section}', 'APP\DetectHookController#detectProjectcase4')->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('section', '[A-Za-z0-9_\-A-Za-z0-9_\-]+');
And second one is
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{clustername}/{projname}', array( 'as' => 'project-with-cluster', 'uses' => 'APP\DetectHookController#detectProjectcase2'))->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+');
I want conditional routes based on the {section} parameter in first route.
The second one doesn't get call when it is supposed to be called as both routes are having same parameters. Can someone suggest me as I am helpless for almost a week.
You can re-structure your route as:
Route::get('/{buy_type}-property/{type}-in-{city}/{location}/project/{projname}/cluster/{clustername}', array( 'as' => 'project-with-cluster', 'uses' => 'APP\DetectHookController#detectProjectcase2'))->where('projname', '[A-Za-z0-9_\-A-Za-z0-9_\-]+')->where('location','[A-Za-z0-9_\-A-Za-z0-9_\-]+');
My route is:
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1');
Route::get('members/{id}/edit', 'Sample2Controller#sampleFn2');
When i click the url link from blade,
Edit
it goes to the first route and calls Sample1Controller#sampleFn1. Why?? Please help..
When I click the link..I want to go the second route and calls Sample2Controller#sampleFn2. Any help?
Thanks in advance.
You need to add route where condition to parameters. First I quess is for stings, the second for integers:
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1')->where([
'name' => '[a-z]+',
'id' => '[0-9]+',
]);
Route::get('members/{id}/edit', 'Sample2Controller#sampleFn2')->where([
'id' => '[0-9]+'
]);
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1');
Both URL's look the same for laravel, in this case, $name is being set to "1" and $id is being set to "edit".
You need to avoid ambiguity by moving the optional parameter to the end
And the status text one level back, in this case:
Route::get('members/edit/{id}', 'Sample2Controller#sampleFn2');
Route::get('members/{id}/{name?}', 'Sample1Controller#sampleFn1');
It's not validating name or id against any specific constraints so you're /1/edit qualifies... to use the same route definitions you can simply reverse the order of the definitions.
try the following:
Route::get('members/{id}/edit', 'Sample2Controller#sampleFn2');
Route::get('members/{name?}/{id}', 'Sample1Controller#sampleFn1');
Route::get('/post-delete/{post_id}',[
'uses' => 'PostController#getDeletePost',
'as' => 'post.delete',
'middleware' => 'auth'
]);
My controller is like this
public function getDeletePost($post_id)
{
$post = Post::where('id',$post_id)->first();
if(Auth::user() != $post->user)
{
return redirect()->back();
}
$post->delete();
return redirect()->route('dashboard')->with(['message' => 'Sucessfully Deleted Taunt']);
}
and my blade.php is like this
Delete
the above code is for delete u c an try it for edit