Laravel 5.2 validation redirect - validation

Is it possible to redirect the user to a POST route in Laravel if validation fails?
Route::get('/order/confirm', ['middleware' => 'auth', 'uses' => 'PageController#orderConfirm']);
Route::post('/order/create', ['middleware' => 'auth', 'uses' => 'PageController#orderCreate']);
Route::post('/order/store', ['middleware' => 'auth', 'uses' => 'PageController#orderStore']);
I have 2 forms. First is confirm the order. Second is form containing the order.
If I give incorrect data in the order, shows an error message.
MethodNotAllowedHttpException in RouteCollection.php line 219:
PS: I'm sorry for my english.

Could you change
Route::post('/order/create', ['middleware' => 'auth', 'uses' => 'PageController#orderCreate']);
to
Route::get('/order/create', ['middleware' => 'auth', 'uses' => 'PageController#orderCreate']);
it will work.

Related

Why does Laravel api's are returning 405 error?

So my scenario is:
Route::group(['middleware' => 'api'], function ($router) {
Route::post('/login', [
'as' => 'login',
'uses' => 'App\Http\Controllers\Admin\CustomerCrudController#login',
]);
Route::post('/register', [
'as' => 'register',
'uses' => 'App\Http\Controllers\Admin\CustomerCrudController#register',
]);
Route::get('/logout', [
'as' => 'logout',
'uses' => 'App\Http\Controllers\Admin\CustomerCrudController#logout',
]);
});
Here Login API is working perfectly using postman but register and logout api's are returning 405 method not allowed error.
Note: I have already checked that I must be using post and get correctly. These API's were also working right previously. I don't know what has made them stop.
In case if someone wants to have a look at controller functions, here is the gist. Looking forward to the suggestions.

Array to string conversion in named route group in laravel 5.6

My named route group is working fine without resource route. But when I am trying to use 'resource route' then getting this error. Would someone help me please, in where I am doing wrong?
My Route Group is -
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => 'auth:admin'], function () {
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Admin\AdminController#dashboard'));
Route::group(['prefix' => 'student', 'as' => 'student.'], function () {
Route::resource('admission', array('as' => 'admission', 'uses' => 'Admin\StudentController'));
}); });
You need to pass resource controller name as string as the second parameter for Route::resource():
Route::resource('admission', 'Admin\StudentController');
You don't need to specify routes names with 'as' => 'admission' because the Route::resource() will do that automatically.

Laravel auto auth?

I need to secure my backend section.
Right now i have i like this:
Route::get('/backend',['middleware' => 'auth', 'uses' => 'HomeController#index']);
Route::get('/backend/users',['middleware' => 'auth', 'uses' => 'HomeController#show']);
Route::get('/backend/users/create',['middleware' => 'auth', 'uses' => 'HomeController#create']);
Route::get('/backend/users/edit/{id}',['middleware' => 'auth', 'uses' => 'HomeController#edit']);
do i need to write the middleware=> auth to everyline and everysite i have in my backend?
Is it somehow possible to define that everything that has 'backend/' should be checked if auth or not?
You can use a Route Group to define middleware and a prefix (among other things). So it could be:
Route::group(['prefix' => 'backend', 'middleware' => 'auth'], function () {
Route::get('/', 'HomeController#index');
Route::get('/users', 'HomeController#show');
Route::get('/users/create', 'HomeController#create');
Route::get('/users/edit/{id}', 'HomeController#edit');
)};

Behaviour of routing in laravel

After trying out this acl tutorial I came across something I can't understand.
In laravel I created my route according to the tutorial and changed it to match laravel's auth controller (as I already used that before by installing it with bestmomo) to:
$router->get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);
Like this it doesn't find my route giving me the error
NotFoundHttpException in RouteCollection.php line 161:
If I add the following route:
Route::get('/login', [
'as' => 'login', 'uses' => 'Auth\AuthController#getRegister'
]);
It works well.
Why do I need to add the second route?
Why can't the first one stand alone?
I think the blog you are following has an error.
The correct way to call a route you need is:
Route::get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);
Note: you can also use a helper method and just call:
get('/', [
'uses' => 'Auth\AuthController#getLogin',
'as' => 'admin.user.login',
'middleware' => ['acl:login']
]);;
The only way I can see the the code you mentioned working is if the following is at the top of the routes file:
$router = app('router');

Laravel 5 advance routing with reserved route keywords

Would like to check say that I have the following routes
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile'));
Route::get('/settings/password', array('as' => 'chgPassword', 'uses' => 'ProfileController#updatePassword'));
Route::post('/settings/password', array('as' => 'postChgPassword', 'uses' => 'ProfileController#postUpdatePassword'));
Route::get('/settings/email/request', array('as' => 'chgEmailRequest', 'uses' => 'ProfileController#updateEmailRequest'));
Route::post('/settings/email/request', array('as' => 'postChgEmailRequest', 'uses' => 'ProfileController#postUpdateEmailRequest'));
Route::get('/logout', array('as' => 'logout', 'uses' => 'ProfileController#logout'));
});
Notice that my first route accepts an optional parameter which will then route the user to a specific profile which it works fine, but when ever i have other routes say that /logout, laravel router will also use the /{profile_url?} route instead of the expected logout route. Is there any way that i can specified something like a reserved keyword like
Route::get('/{profile_url?}', array('as' => 'profile', 'uses' => 'ProfileController#getProfile')
->except('settings', 'logout'));
something like that? Ho that someone can enlighten me with this issue.
Because you put a wildcard {profile_url?} at the first place, Laravel will ignore the rest. So be careful when using wildcard routes. you should put the least specific route in the last place, Lavarel will check all of specific routes. If it doesn't match, it will go to the wildcard route. For example :
Route::group(['middleware' => 'auth'], function(){
Route::get('/{profile_url?}',...); // Lavarel do this
Route::get('/logout',...); // ignore this
});
Route::group(['middleware' => 'auth'], function(){
Route::get('/logout',...); // do this if it matches
Route::get('/{profile_url?}',...); // else do this
});

Resources