Laravel Route Resource both GET and POST - php

I have this Route
Route::group([ 'middleware' => ['auth','lang']], function() {
// SETTINGS
Route::namespace( 'Settings' )->prefix( 'settings' )->group( function () {
// INDEX
Route::get( '/', 'SettingsController#index' );
// ACCOUNTS
Route::resource( 'accounts', 'AccountController', ['only' => ['index','store','edit','update']] );
// TAGS
Route::resource( 'tags', 'TagController', ['only' => ['index','destroy']] );
// PROFILE
Route::get('profile', 'ProfileController#index');
Route::post('profile', 'ProfileController#update');
});
Any way I can join the two PROFILE ones into one that is resource? Whenever I try using Route::resource( 'profile', 'ProfileController', ['only' => ['index','update']] ), it gives me an error that the method is not allowed - 405 (Method Not Allowed). I think it just doesn't find the update one? I am really not sure what might be the issue.

This is happening because in the case of resourceful controllers, a post would be defaulted to a store method, not update.
So you are posting to the store method, which is not defined, giving you the 403 method not allowed.
To solve this, either change your request to a PUT or change your code to Route::resource( 'profile', 'ProfileController', ['only' => ['index','store']] ) Keep in mind, if you do this, you have to move the contents of your update function to store.
For more information, checkout https://laravel.com/docs/5.5/controllers#resource-controllers

Related

Admin route group with prefix, middleware and named routes

I have some named routes in a controller named VehicleController:
vehicle.index
vehicle.show
And then I have an admin section, where I have defined a route group with prefix and middleware. In this section I have a resource controller name AdminVehicleController to handle CRUD tasks for the Vehicle (not sure if this is best practice) with the following routes:
vehicle.index
vehicle.create
vehicle.store
...
However these named routes are conflicting. My routes web.php looks like this for now:
Route::get('vehicles', 'VehicleController#index')->name('vehicle.index');
Route::get('vehicle/{vehicle}', 'VehicleController#show')->name('vehicle.show');
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin'], function () {
Route::get('/', 'AdminDashboardController#index');
Route::resource('vehicle', 'AdminVehicleController');
});
If I add 'name' => 'admin' to the Route::group() array, the route names will be adminvehicle.index and not admin.vehicle.index.
What is the correct way to combine all these parameters in the route?
Try to use as parameter for your admin group
Route::group(['prefix' => 'admin', 'middleware' => 'is.admin', 'as'=> 'admin.'], function () {
Route::get('/', 'AdminDashboardController#index')->name('dashboard');
Route::resource('vehicle', 'AdminVehicleController');
});
Reference Link
Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.
Route::resource('vehicle', 'AdminVehicleController', [
'names' => [
'index' => 'admin.vehicle.index',
// etc...
]
]);

Route login not defined (Multi-auth)

I have problem with multi-auth. Postman says error:
problem:
Route [login] not defined.
I understand it doesn't know which one to route?
I try to make laravel passport api so I dont need any automatic redirections.. I try to create multi-auth.
Like Admins, Stylists, Freelancers, clients..
Every route have own login and registration and other routes..
I tryed to ungroup but this isn't good solution..
Route::group(['middleware' => ['json.response']], function () {
Route::get('/freelancer/{profile}', 'API\FreelancerController#profile'); // Guests can also see profiles..
Route::group(['prefix' => 'admin', 'namespace' => 'API', 'middleware' => 'auth:admin'], function() {
Route::post('/login', 'AdminController#login');
});
Route::group(['prefix' => 'freelancer', 'namespace' => 'API', 'middleware' => 'auth:freelancer'], function() {
Route::post('/login', 'LoginRegisterController#login');
Route::post('/register', 'LoginRegisterController#freelancerRegister');
});
Route::group(['prefix' => 'stylist', 'namespace' => 'API', 'middleware' => 'auth:stylist'], function() {
Route::post('/login', 'LoginRegisterController#login');
Route::post('/register', 'LoginRegisterController#stylistRegister');
});
Route::group(['prefix' => 'client', 'namespace' => 'API', 'middleware' => 'auth:client'], function() {
Route::post('/login', 'ClientController#login');
Route::post('/register', 'ClientController#clientRegister');
});
});
If I'm http://api.mywebsite.com/freelancer/register then I can register freelancer account..
Or If I'm http://api.mywebsite.com/admin/login then I can only login to admin dashboard.
POSSIBILITY 1
Please check what route you are targetting when creating a login request from POSTMAN.
I believe you are using a route like
http://api.mywebsite.com/login
whereas you should be using one of the following:
http://api.mywebsite.com/admin/login
http://api.mywebsite.com/freelancer/login
http://api.mywebsite.com/stylist/login
http://api.mywebsite.com/client/login
POSSIBILITY 2
If above isn't the case and you are targetting a good route then check your method, it should be POST, by default POSTMAN uses GET method when you initialize a request.
However, chances for this mistake is less as it would give rather METHOD NOT EXIST error than ROUTE NOT FOUND but it's worth a try.

Laravel Add Post Route to Resource Route

I have a Laravel 5.2 app that is using Resource routes. I have one as follows:
Route::resource('submissions', 'SubmissionsController');
I want to add a new Post route to it for a sorting form on my index page.
Route::post('submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
I have placed the Post route above my Resource route in my routes.php.
However, a validation Request named SubmissionRequest that is meant for forms within the Submission Resource is being executed on my new Post route. Here is my SubmissionsController Method.
public function index(SortRequest $req)
{
$submission = new Submission;
$submission = $submission->join('mcd_forms', 'mcd_forms.submission_id', '=', 'submissions.id')->where('user_id', Auth::user()->id);
$data['sort_types'] = [
'name' => 'Name',
'form_type' => 'Type'
];
$data['direction'] = ( !empty($req['asc']) ? 'asc' : 'desc' );
$data['dataVal'] = ( !empty($req['sort_type']) ? $req['sort_type'] : 'submissions.id' );
$submission->whereNull('submissions.deleted_at')->orderBy(
$data['dataVal'],
$data['direction']
);
$data['submissions'] = $submission->get();
return view('submissions.index')->with($data);
}
So, when submitting the sorting form from my index page, it is running the SubmissionRequest validation even though I am specifically calling the SortRequest validation. What am I doing wrong?
I resolved it.
Since my Post route was conflicting with my Get route for submissions.index I added below the Resource route the following:
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
This allows the route to accept both Get and Post requests by overriding the automatically generated one.
The documentation is here: https://laravel.com/docs/master/routing#basic-routing
Route::match(['get', 'post'], 'submissions', [
'as' => 'submissions.index',
'uses' => 'SubmissionsController#index'
]);
in laravel 5 its conflict with #store action

Named Routes Conflict Laravel 5.2

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Laravel 4, group prefix requires changing route names. Really?

So I've been working on an app that lived on the domain root and now has to work on /admin. So URLs like domain.com/[resource] should now be domain.com/admin/[resource]. I didn't thought this very well before since I assumed that this had to be a very easy fix on Laravel. After all, that's one of the main reasons for not hardcoding routes, right?
So my routes.php file looked something like:
Route::group(['before' => 'auth'], function() {
Route::resource('books', 'BooksController');
... more resources here ...
});
Going through the docs I found that 'prefix' => 'admin' would do the trick:
Route::group(['prefix' => 'admin', 'before' => 'auth'], function() {
Route::resource('books', 'BooksController');
... more resources here ...
});
But it turns out that every route name get's changed from books.{action} to admin.books.{action} which requires me to change the whole app. Regexing would be dangerous and doing it manually would be annoying. Laravel was supposed to help with this! Or am I missing something?
This is untested, but after looking on the documentation for resource controllers it seems as though you can manually set their names. I'm assuming Laravel automatically namespaces grouped resource controller routes to avoid name collision, but you can override this to avoid going back through the rest of your app (just beware of future name collision):
Route::resource(
'books',
'BooksController',
array(
'names' => array(
'index' => 'photo.index',
'create' => 'photo.create',
'store' => 'photo.store',
'show' => 'photo.show',
'edit' => 'photo.edit',
'update' => 'photo.update',
'destroy' => 'photo.destroy',
)
)
);
Shorter Method:
Just define a quick method at the top of your routes.php file to shorten up this repetitive task of creating an array of route names. Still not the greatest solution, but I believe its the only thing you can do with how Laravel has this set up.
function createRouteNames($resource) {
$names = array();
$types = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
foreach($types as $type) {
$names[$type] = $resource . '.' . $type;
}
return $names;
}
Route::resource('books', 'BooksController', ['names' => createRouteNames('books')]);
Note: [] === array() and may not be supported on older PHP's, meaning you may need to replace them with the old syntax.

Categories