Laravel 6.0 How to use Route:resource get route same folder controller - laravel

I have question and I'm can't find a solution in document
I use command
php aritsan make:controller Backend\ProductController --resource --Model=Model\Product
So, I will need route same location file controller
I use
Route::resource('/backend/product','Backend\ProductController');
after, run a command
php artisan route:list
and this result
But, I don't need this
I think should be
+--------+-----------+----------------------------------+-----------------+------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+----------------------------------+-----------------+------------------------------------------------------------+------------+
| | GET|HEAD | backend/product | backend.product.index | App\Http\Controllers\Backend\ProductController#index | web |
| | POST | backend/product | backend.product.store | App\Http\Controllers\Backend\ProductController#store | web |
| | GET|HEAD | backend/product/create | backend.product.create | App\Http\Controllers\Backend\ProductController#create | web |
Route name should be backend.product.index
I find a solution. but not happy.
Route::resource('/backend/user','Backend\UserController')->names([
'index' => 'backend.user.index',
'store' => 'backend.user.store',
'edit' => 'backend.user.edit',
'update' => 'backend.user.update',
'destroy' => 'backend.user.destroy',
]);
Documents resource names

The command for creating Model Controller with resource
php artisan make:controller Backend\ProductController --resource --Model=Model\Product
Change web.php and use prefix, namespace, as
Route::group(['prefix' => 'backend','namespace'=>'Backend','as'=>'backend.'], function () {
Route::resource('product','ProductController');
});
Now use
php artisan route:list
backend.product.index
backend.product.create
backend.product.show
backend.product.destroy
backend.product.update
backend.product.edit

refer Routing page in Laravel Documents for more details.
https://laravel.com/docs/5.8/routing#route-group-prefixes

Related

Laravel - Can I pass a parameter with apiResource to the index function in the controller?

Is there a way to pass a parameter into an apiResource route? I want only the index to take an userId. Adding {id} to the route does not seem to help. I looked at the laravel website but couldn't find anything about adding custom parameters.
I had a previous post about this but to make the question more clear I made a new post. I'll delete the previous post later today.
https://laravel.com/docs/5.7/controllers#resource-controllers
Route::apiResource('campaigns', 'CampaignController');
No theres no way to do thats but you can make this, and is the same.
Route::apiResource('campaigns', 'CampaignController',['except' => 'index']);
Route::get('campaigns/{id}', [
'as' => 'campaigns.index',
'uses' => 'CampaignController#index'
]);
The index don't have an {id} by default.
If you want in laravel 8 change the naming of route parameters in apiResource just use parameters like:
Route::apiResource('users', AdminUserController::class)->parameters([
'users' => 'id'
]);
This will generate these routes:
| POST | api/users | users.store | App\Http\Controllers\AdminUserController#store
| GET|HEAD | api/users | users.index| App\Http\Controllers\AdminUserController#index
| GET|HEAD | api/users/{id} | users.show | App\Http\Controllers\AdminUserController#show
| DELETE | api/users/{id} | users.destroy| App\Http\Controllers\AdminUserController#destroy
| PUT|PATCH | api/users/{id} | users.update | App\Http\Controllers\AdminUserController#update

Laravel routes prefix and ressources resulting in 404 not found

I've this Laravel routing issue. I don't really understand why my route:list tells me that an {} empty is appended to the URL? I believe this is the reason why my calls returns 404 not found.
I'd like DepartmentController to be inside my grouped body as I need the ID for other purposes. If I move ressource outside the prefix/group this scenarie works, but the other dosen't. This is my preferred way of structuring my routes, but it troubles me that / dosen't just use the prefixed URL, but it for some reason append it with {}
What am I doing wrong?
Calling URL: /department/1/edit
Result: 404 Not Found
Routes:
Route::prefix( 'department/{department_id?}' )->group( function () {
Route::resource( '/', 'DepartmentController' );
}
php artisan route:list:
| | GET|HEAD | department/{department_id?}/{} | show | App\Http\Controllers\DepartmentController#show | web |
| | PUT|PATCH | department/{department_id?}/{} | update | App\Http\Controllers\DepartmentController#update | web |
| | DELETE | department/{department_id?}/{} | destroy | App\Http\Controllers\DepartmentController#destroy | web |
| | GET|HEAD | department/{department_id?}/{}/edit | edit | App\Http\Controllers\DepartmentController#edit | web |
| | GET|HEAD | department/{department_id?}/create | create | App\Http\Controllers\DepartmentController#create | web |
| | POST | department/{department_id?} | store | App\Http\Controllers\DepartmentController#store | web |
| | GET|HEAD | department/{department_id?} | index | App\Http\Controllers\DepartmentController#index | web |
Update:
If I make a custom route like this:
Route::get( 'customedit', 'DepartmentController#editasddas' );
and request the url: /department/1/editasddas. It works as it's suppose to, but there is actually a reason why I'm using the ressource: to keep the routes as clean as possible. The ressource-routes have been implemented for that reason aswell, and I just need to implement the basic CRUD operations. Is this a bug in Laravel, or is this basically not possible? - really strange I think. It's not that complex.
I think you have this issue because how Route::resource creates subroutes itself (automatically adding a resource parameter within URLs, the parameter being {} at the end).
Also, note you are currently generating a index route with a department parameter, and that's not really useful.
Best solution for me is to move out your parameter:
Route::prefix( 'department' )->group( function () {
Route::resource( '/', 'DepartmentController' );
});
In the other hand, the department_id parameter will not be facultative. And you will need to add the parameter within each other custom routes (but that's what Route::resource does with its own routes after all).
Second one is to keep your prefix and declare each route individually. But you will need to change the default route names because department.index and department.show will have the exact same methods (GET and HEAD) and URLs (department/{department_id}).
Route::prefix('department/{department_id}')->group(function() {
Route::match(['get', 'head'], '/', 'DepartmentController#index')->name('department.index');
Route::match(['get', 'head'], '/show', 'DepartmentController#show')->name('department.show');
/* Declare all the others. */
});
The Route::resource method alone will achieve what you're looking for:
Route::resource( 'department', 'DepartmentController' );
Check the docs on this here, https://laravel.com/docs/5.8/controllers#resource-controllers

Laravel 5.6 php artisan route:list shwoing only api middleware why?

I am trying to make api from laravel5.6, its works but both are not wotrking, I am not able to run website, because web route not working
in route list shpwing on api middleware why?
$ php artisan route:list
+--------+----------------------------------------+--------------------+----------+--------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------------------------------------+--------------------+----------+--------------------------------------------------+------------+
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | api/en/v1/gettoken | gettoken | App\Http\Controllers\Api\ApiController#gettoken | api |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | api/en/v1/login | login | App\Http\Controllers\Api\UserController#login | api |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | api/en/v1/register | register | App\Http\Controllers\Api\UserController#register | api |
Write your routes in routes/web.php. all routes in api.php are uses the api middleware.

laravel routing in web.php for self referring table

I am using laravel 5.6
I have a self referring table called "directory"
$table->increments('id');
$table->integer('parent_id')->unsigned()->default(null)->nullable();
$table->string('folder', 64);
$table->timestamps();
$table->unique(['parent_id', 'folder']);
For a root directory the "parent_id" will be null
How to create a resource route in web.php ? Something like:
Route::resource('directory.childdirectory','DirectoryController')->name('directory', 'dir_root_id', 'dir_child_id');
Is there any example CRUD for self referential relation?
How can a create a resource route in web.php for a self referential relationship?
The problem in short for a self referring table was how to pass the "parent" and "child" id to the controller. I did that by defining a resource controller in the routing table (web.php for laravel 5.6) with an additional parameter {id}
Route::resource('{id}/directory', 'directoryController');
Command php artisan route:list will look like the code below, where {id} is the parent directory and {directory} is the child directory
| POST | {id}/directory | directory.store
| GET|HEAD | {id}/directory | directory.index
| GET|HEAD | {id}/directory/create | directory.create
| GET|HEAD | {id}/directory/{directory} | directory.show
| PUT|PATCH | {id}/directory/{directory} | directory.update
| DELETE | {id}/directory/{directory} | directory.destroy
| GET|HEAD | {id}/directory/{directory}/edit | directory.edit

Laravel 5.2 some routes doesn't work

I have this route:
Route::get('/sites', 'SitesController#index');
and when i run http://localhost:8880/sites it responds:
The Browser response (404 error): The requested resource /sites was not found on this server.
The Terminal Response : Invalid request (Unexpected EOF)
When i change the route to:
Route::get('/premium-sites', 'SitesController#index');
And run it in my browser
http://localhost:8880/premium-sites.
Everything works like a charm.
I don't use anywhere else this route (/sites) and
all other 20 routes i have, work fine except for this.
I can't understand what kind of bug is this and i can't find a way to fix it.
Update
This is my route list
+--------+----------+-------------------+-----------------+-------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------------------+-----------------+-------------------------------------------------+------------+
| | GET|HEAD | categories/{type} | categories.show | App\Http\Controllers\CategoriesController#index | web |
| | GET|HEAD | sites | sites.show | App\Http\Controllers\SitesController#index | web |
+--------+----------+-------------------+-----------------+-------------------------------------------------+------------+
I also changed the route to this:
Route::get('/sites', [
'as' => 'sites.show',
'uses' => 'SitesController#index'
]);
and my controller to:
public function index()
{
return 'test route';
}
The response is :
And the terminal response:
[Tue Aug 30 01:56:15 2016] ::1:61591 [404]: /sites - No such file or directory
[Tue Aug 30 01:56:15 2016] ::1:61593 [200]: /favicon.ico
[Tue Aug 30 01:56:38 2016] ::1:61594 Invalid request (Unexpected EOF)
I am probably late for this answer. Anyways could be useful for someone.
Had this issue the other day, the reason for me was due to the existence of a the folder name "X" in the public directory which resembles the route name "X".
Most probably you are having a folder called "sites" in public directory, Either rename that or the one in the routes file
It appears that there may be some conflict somewhere in one of your routes file. Here is the setup I have and it is working just fine.
routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/sites', 'SitesController#index');
});
route:list
php artisan route:list
+--------+----------+-------+------+--------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-------+------+--------------------------------------------+------------+
| | GET|HEAD | sites | | App\Http\Controllers\SitesController#index | web |
+--------+----------+-------+------+--------------------------------------------+------------+
Here is my controller
<?php
namespace App\Http\Controllers;
class SitesController extends Controller
{
public function index()
{
return 'Test Route';
}
}
To debug this, I would start with xdebug to see if you can break at the route to see what is going on and follow the trace. if you do not have xdebug set up, comment out all routes except for the /sites route and see if it works then. If it does, one of your other routes is in the way. Begin by uncommenting routes one by one or group by group and when the error re-surfaces, you are closer.
Maybe Route::get('/sites', 'SitesController#index'); is involved
better use this route:
// Sites
Route::resource('/sites', 'SitesController');
for example the route :
// Home
Route::resource('/home', 'HomeController');
have :
GET|HEAD | home | home.index | App\Http\Controllers\HomeController#index
POST | home | home.store | App\Http\Controllers\HomeController#store
GET|HEAD | home/create | home.create | App\Http\Controllers\HomeController#create
DELETE | home/{home} | home.destroy | App\Http\Controllers\HomeController#destroy
PUT|PATCH | home/{home} | home.update | App\Http\Controllers\HomeController#update
GET|HEAD | home/{home} | home.show | App\Http\Controllers\HomeController#show
GET|HEAD | home/{home}/edit | home.edit | App\Http\Controllers\HomeController#edit

Resources