How to declare routes with resources in Laravel 5.2 - laravel

I have some routes in routes.php in laravel
// Code for rounting admin panel
Route::resource('/admin','Admin\LoginController#index');
Route::resource('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movies','Admin\MovieController#index');
Now when I access url http://localhost/askspidy/admin I want to show login page and it works, but when i access url http://localhost/askspidy/admin/dashboard it should go to dashboard but it's showing me login page only.
I know this is because when it found /admin in any url it's bydefault goes to the route
Route::resource('/admin','Admin\LoginController#index');
I know it's assuming that (/admin) is route to controller and (/dashboard) is the function declared in the controller but I want routing like this only so is there any other solution for this problem.

A RESTful Resource Controller takes over the responsibility of each action. You only need to list the name and the controller:
Route::resource('photo', 'PhotoController');
If you wanted to only use the index method, you’d list it like this:
Route::resource('photo', 'PhotoController', ['only' => [
'index'
]]);
However, it looks like two of your routes are not suitable for resources (login and dashboard), as they should relate to editing a model.
You should instead just use a get() resource instead.
From the docs:
Route::get('user/{id}', 'UserController#showProfile');
So in your case, it would be:
Route::get('/admin','Admin\LoginController#index');
Route::get('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movie','Admin\MovieController');

Related

Set tab name in redirect to route in laravel

in laravel 8 i will redirect the user to a route after doing the query.
There are many tabs on the page where I want the user to be transferred
I wrote the code like this
(Of course I know it's wrong, but I wanted to try, and yet I did not think of another way
return redirect()->route('admin.auth.user.show', $user . "#edit-information")->withFlashSuccess(__('The user was successfully updated.'));
I get an error with this code
What is your solution?
What you can do to activate for example bootstrap tab based on #web paramater is use the redirect() method like this:
return redirect()->route('backend.settings.show', ['eshop' => $eshop->id, '#homeImages']);
Eshop is regular route parameter but '#homeImages' is added to the end of the route which is then picked up by the js on page load.
In Laravel there is a withFragment method, which add a fragment identifier to the URL.
return redirect()->route('admin.auth.user.show', ['user' => $user])->withFragment('edit-information');
https://laravel.com/api/8.x/Illuminate/Http/RedirectResponse.html#method_withFragment

How to write laravel 5.8 controllers routes similar to laravel 5.1

Having more than 20 controllers. It's very difficult to set each and every routes for add, edit and delete (also having more actions).
This is my laravel 5.1 routes.php :
Route::controllers([
'user' => 'UserController',
'taxes' => 'TaxController',
]);
Is there any way to support these routes in laravel 5.8?
You can use the Resource Controller and implement in routes/web.php. It will autogenerate the name for the route
//web.php
Route::resource('user', 'UserController');
Route::resource('taxes', 'TaxController');
Edit 1
If you want to exclude show method of the controller for the resource, You can add array inside the except method.
Route::resource('taxes', 'TaxController', [
'except' => ['show']
]);
Further, if you want to get only selected options, You can use only.
Route::resource('taxes', 'TaxController', [
'only' => ['index', 'create', 'store', 'edit']
]);
The controllers method was deprecated in Laravel 5.2. From the upgrade guide:
Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file.
1) Use Resource Routes
Provided that your controllers use the standard index, store, show etc methods you can simply use resource routes. For example:
Route::resource('user', 'UserController');
However if you want to exclude certain methods you can add them to the resource. For example:
Route::resource('user', 'UserController', ['except' => 'show']);
2) Declare Routes Explicitly
You can follow the Laravel 5.2 upgrade guide as above and instead declare each route explicitly.
3) Create a Macro
The Laravel router is Macroable. This means that you can add your own methods to it. For example, in your app service provider you could have the following:
Illuminate\Routing\Router::macro('controllers', function ($routes) {
// Create your own implementation of the controllers method.
});
This allows you to create your own implementation of the controllers method which means you wouldn't need to alter your routes or controllers, but you may need to dive in and look at Laravel's route handling to understand how to implement this.
I hope this helps.
You can use in the array, In as you call using routes. like {{route('claimsubmit')}}
Route::resource('claimform',array('as'=>'claimform','uses'=>'UserController#claimform');

Laravel 5.6 additional Route::resource() Parameters

I would like to know how to add additional parameters to Laravel's Route Resource without using Query Strings.
I created a controller (CustomerController) with all the built-in resources and then, added the following route:
Route::resource('customers', 'CustomerController');
What i would like to do is add additional parameters to some of the default resources without creating custom routes or using query strings. For example:
Default resource with optional parameter (index):
public function index($page = 0)
{
//...
}
Desired URL:
http://www.example.com/customers
http://www.example.com/customers/{page}
I tried the following, but i get a not found exception (NotFoundHttpException):
Route::resource('customers', 'CustomerController')->parameters([
'index' => 'page'
]);
Is this possible? If so, how can i accomplish it?
Resource Controllers must implement a defined set of methods which are then mapped to the appropriate HTTP verb and path by the router. These methods, paths and verbs form part of a contract that cannot be adjusted, otherwise working with a Laravel application that implements Resource Controllers would be a headache.
Resource Controllers excel in providing the same experience across all Laravel applications, if your application requires behaviour that isn't supported out of the box by Resource Controllers then it is a sign that you should not be using them and should instead register your routes manually.
If you have just one route that needs to implement custom behaviour then you can register some methods instead of all and then choose to register a custom route to your Resource Controllers method, something like:
Route::resource('customers', 'CustomerController')->except([
'index'
]);
Route::get('/customers/{page?}', 'CustomerController#index');
The documentation on Resource Controllers covers except and only.
Try this :
Route::resource('customers', 'CustomerController')->parameters([
'customers' => 'page'
]);
The example above generates the following URIs for the resource's show route:
/customers/{page}
you set the name route: 'index' replace it by the variable : 'customers' name of your ressource

Laravel: Automatically add parameter to urls

I have an application in which you can create a service and a service can have its own partial view.
So I created a route group with {service} prefix:
Route::group(['prefix' => '{service}', ... ], ... ).
// http://.../my-service/my-url
However, in order to know in which service the user is I need to add the service in every single route I have in my application. So I have done a middleware that shares $service to every view:
view()->share(['service' => $service])
But I don't know how to add $service prefix to every route without explicitly adding it. I would like doing something like
route()->prefix(['service' => $service])
and then every route have the prefix $service:
url("myurl") // -> url("$service/my-url") or
route('my-route')
Any idea?
EDIT:
Finally I decided to create a ServiceType model, create a middleware with a parameter and set to my route groups. Then in view I offer the user to switch between services of the same type.
Not is what I was looking but it's OK for now.
Question is still open if anyone knows the answer.
Put all your routes you want prefixed in a group and then add the prefix
Laravel docs on route prefixes

Laravel-4: Difference between RESTful Controllers and Resource Controllers in Laravel

Someone can please explain what is the difference between RESTful Controllers and Resource Controllers in Laravel ? I also have some Questions-
when should I use RESTful Controllers and when Resource Controllers?
Is there any naming convention Of Controller action for RESTful Controllers and Resource Controllers ?
If I use RESTful Controllers how could I define route for our controller ?
For building API which Controller Method is the best ?
Laravel Resource Controllers are defined as Route::controller('users', 'UserController'); while Restful Controllers are defined as Route::resource('photo', 'PhotoController');.
A restful controller follows the standard blueprint for a restful resource which mainly consists of:
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy
While the resource controller isn't opinionated like the restful controller. It allows you to create methods directly from you controller and it all gets automatically mapped to your routes:
public function getIndex()
{
// Route::get('/', 'Controller#getIndex');
}
public function postProfile()
{
// Route::post('/profile', 'Controller#postProfile');
}
Will automatically have the routes like Route::post('/profile', 'Controller#postProfile'); without explicitly defining it on the routes, much more of a helper if you will to avoid very long route files.
Doing php artisan routes will show you all your routes. You can test stuff out and use that command to see what routes gets automatically generated.
They are different concepts. In laravel, a resource controller defines all the default routes for a given named resource to follow REST principles.
So when you define a resource in your routes.php like:
Route::resource('users', 'UsersController');
The only thing Laravel does is define for you this routes:
Verb Path Action Route Name
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy
And expects that you define those methods on your controller. You can also use only/except clauses to remove unneeded routes:
Route::resource('user', 'UserController', ['except' => ['destroy']]);
More on this on Laravel's documentation.
It's just a distinction about the routing declaration. Instead of using one of those, manually define all of your routes.
Route::get(...);
Route::post(...);
Route::put(...);
Route::delete(...);
Route::patch(...);
It makes your routes file authoritative, easy to understand, and less buggy.
The documentation currently shows RESTful and Resource controllers to refer to the same thing.
Route::resource('resource', 'ResourceController');
It defines the routes for the following http request verbs mapped to the URI, controller action, and route. This allows you to use the predefined route names to connect to predefined controller actions and will map resource_id to {resource} as shown.
Verb URI Action Route Name
GET /resource/index.blade.php index resource
GET /resource/create.blade.php create resource.create
POST /resource store resource.store
GET /resource/{resource}/show.blade.php show resource.show
GET /resource/{resource}/edit.blade.php edit resource.edit
PUT/PATCH update resource.update
DELETE destroy resource.destroy
The term Implicit Controller seems to be the term to specify the use of
Route::controller('resource', 'ResourceController');
which will magically connect all routes to to ResourceController so that the http request verb (get/post) is prefixed in the function name used in the controller. This maps any URI to the controller action (function) with (get/put) in front but does not map resource_id to {resource} or route names.
class UserController extends BaseController {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
maps to
Verb URI Action Route Name
GET /index getIndex
POST /profile postProfile
GET /login anyLogin
POST /login anyLogin
DELETE /login anyLogin
It's up to you to decide which method to use if any for routing. There is some debate as to what is useful and if routing is even worth the confusion it can cause.
RESTful Resource Controllers
Resource controllers make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages "photos" stored by your application. Using the controller:make command via the Artisan CLI and the Route::resource method, we can quickly create such a controller.
To create the controller via the command line, execute the following command:
php artisan controller:make PhotoController
Now we can register a resourceful route to the controller:
Route::resource('photo', 'PhotoController');
This single route declaration creates multiple routes to handle a variety of RESTful actions on the photo resource. Likewise, the generated controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle.
Actions Handled By Resource Controller
http://laravel.com/docs/5.0/controllers#restful-resource-controllers

Resources