I am beginer with Laravel, I need help please.
In my edit user page, I have 3 forms.
But I can not do in my rooter how to make it work.
My code (in Routes) :
<?php
// update role
Route::post('edit/{id}', [
'as' => 'user_post_update_role',
'uses' => 'UserController#updateRole'
]);
// update infos
Route::post('edit/{id}', [
'as' => 'user_post_update',
'uses' => 'UserController#update'
]);
// update password.
Route::post('edit/{id}', [
'as' => 'user_post_update_password',
'uses' => 'UserController#updatePassword'
]);
?>
But the last rule, Block others.
How to do ?
Thank
The only way to do this, would be to have a single route, and have the controller that handles that route identify which form was submitted.
What you're basically doing with those routes, is defining three routes that for all intents and purposes, are identical. Therefore, it'll only use the last one that was created as each new one, overwrites its predecessor.
Alternatively, you could do as it says in the comment on your post, use edit/{id} for updating user info, edit/password/{id} for updating password, and edit/role/{id} for updating the role. You could even break these out into separate methods entirely, or separate sections.
Of the many ways you can achieve this, your chosen method is unfortunately not amongst them.
Edit your route like this:
// update role
Route::post('edit/{id}/role', [
'as' => 'user_post_update_role',
'uses' => 'UserController#updateRole'
]);
// update infos
Route::post('edit/{id}/main', [
'as' => 'user_post_update',
'uses' => 'UserController#update'
]);
// update password.
Route::post('edit/{id}/password', [
'as' => 'user_post_update_password',
'uses' => 'UserController#updatePassword'
]);
So you have unique routes and don't need complicate if else statements.
Related
I am trying to pass 3 variables values inside an html link that uses route to access a named routed with this code:
{{$nomos->name}}
also the route function is
Route::get('gegonota/{gid?}/{cid?}/{nid?}', ['uses' => 'GegonosController#index', 'as' => 'gegonota']);
but it doesnt work right.
How can i do it.
thanks
you can try this.
{{$nomos->name}}
Just remove ? from your routes params so it will be :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index',
'as' => 'gegonota'
]);
if you want to use ? you need to provide a default value check the docs
and if you use Laravel v5.4 you need to change your route to :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index'
])->name('gegonota');
I have a list of routes for my Laravel application. Now, I want to return the same page as is returned on / for all routes that are not listed. I.e.
/hello/world <-> binded to "Hello world" page
/blah/blah <-> binded to "Blah Blah" p
Now, no matter what user requests (GET) and how many slashes in the request are (/asdfda/sadfasdf/asdfasdf, eafsadfsda, asdfadsfasd/adsfsdaf), I want to return the same page. I do not want it to be 404 though, as I want to keep that a legal route.
I tried
Route::get('{all?}', [
'as' => 'spa.index',
'uses' => 'SPAController#index',
]);
But that works only if there are no slashes.
Do someone knows how to do that?
Alright, I found solution by myself. I'll keep the question for future generations. This will return the same page no matter what route user requests
Route::get('{all?}', [
'as' => 'spa.index',
'uses' => 'SPAController#index',
])->where('all', '([A-z\d-\/_.]+)?');
Route::get('/{foo}/{bar}', [
'as' => 'spa.index',
'uses' => 'SPAController#index',
]);
and in SPAController and action index add:
public function index($foo,$bar){
return view($foo.' '.$bar);
}
I am using antonioribeiro/tracker laravel package to Store stats.
Now,I have a routes for Admin directory like this :
Route::group(
array (
'middleware' => 'IsAdmin',
'as' => 'admin::'
),
function () {
Route::get('desktop', [ 'uses' => 'DesktopController#index']);
//some Other Routes
}
);
And I do not want to track this routes and sub routes.
For that I change do_not_track_routes option like this :
'do_not_track_routes' => [
'admin.*',
'tracker.stats.*',
],
But seems this does not work and with every visits Admin or sub Admin directories , add new sessions (or visits) to tracker_sessions table.
You've specified not to track admin.*, but your routes are going to be named admin::*.
You need to either change your route group to be 'as' => 'admin.', or you need to change your do_not_track_routes to exclude 'admin::*'.
I'm doing a simple project. I want it to be as minimal as possible so I tried to create system where I can create pages and they're placed at localhost/{page?}
But, here's the problem. I also want some routes to be defined (e.g. route /blog) like below.
Route::get('/{page?}', ['as' => 'root', 'uses' => 'SiteController#getRoot']);
Route::get('blog/{slug?}', ['as' => 'blog.post', 'uses' => 'BlogController#getPost']);
Route::get('blog/page/{page}', ['as' => 'blog.page', 'uses' => 'BlogController#getPage'])->where('page', '[0-9]+');
With this setup, it only uses the first route.
My question is. Is there a way to do this? Or, is this beyond capabilities of Laravel?
Thank you for any help.
Yeah, place your first route as the last route. That way it will get picked up last. You may also need to place the blog/{slug?} before that one as well so blog/slug/{page} is first.
Route::get('blog/page/{page}', ['as' => 'blog.page', 'uses' => 'BlogController#getPage'])->where('page', '[0-9]+');
Route::get('blog/{slug?}', ['as' => 'blog.post', 'uses' => 'BlogController#getPost']);
Route::get('/{page?}', ['as' => 'root', 'uses' => 'SiteController#getRoot']);
Basically what happens is the most basic route is picking up the other routes because there is no reason for it not to and it technically matches the route even though it's not the route you want. Putting the most specific routes first usually handles this issue.
Try reordering them:
Route::get('blog/page/{page}', ['as' => 'blog.page', 'uses' => 'BlogController#getPage'])->where('page', '[0-9]+');
Route::get('blog/{slug?}', ['as' => 'blog.post', 'uses' => 'BlogController#getPost']);
Route::get('/{page?}', ['as' => 'root', 'uses' => 'SiteController#getRoot']);
otherwise they get "overwritten"
How do you add an regexp on a Route::group? On a normal Route::verb you add ->where('segment', 'regex') to the end, but how do you do it on a group?
I would like something like this (not working as ->where() on Route::group is invalid):
Route::group(['prefix' => '{profileId}'], function(){
Route::get('/', [
'as' => 'profileShow',
'uses' => 'ProfileController#getShow'
]);
Route::post('ny-anvandare', [
'as' => 'profileAccessNew',
'uses' => 'ProfileController#getAccess'
]);
})->where('profileId', '[0-9]+');
You'll need to use the Enhanced Router package by Jason Lewis: https://github.com/jasonlewis/enhanced-router
It introduces much requested enhancements to Laravel's Routing components, including regex filters on groups.
I am not 100% sure if it is Laravel 4.1-ready, but if you're using 4.0, you should be good to go.