I am unable to get the index route of my resource when using some prefix,
when my route is this:
Route::resource('subjects', 'SubjectController')->middleware('auth:admin');
i got this route listing:
| subjects | subjects.index
| subjects | subjects.store
| subjects/create | subjects.create
| subjects/{subject} | subjects.update
| subjects/{subject} | subjects.destroy
| subjects/{subject} | subjects.show
| subjects/{subject}/edit | subjects.edit
But when i add a prefix like this:
Route::prefix('admin')->group(function () {
Route::resource('subjects', 'SubjectController')->middleware('auth:admin');
});
The index route disappears, and routes list become this:
| admin/subjects | subjects.store
| admin/subjects/create | subjects.create
| admin/subjects/{subject} | subjects.update
| admin/subjects/{subject} | subjects.destroy
| admin/subjects/{subject} | subjects.show
| admin/subjects/{subject}/edit | subjects.edit
Got the solution. actually there was another route ('subjects') in my web.php. I needed to remove this, since they both were colliding.
Related
I'm using default laravel auth , I have multiple routes with prefix group but when I use other routes/urls but giving the same page on every route which is View page the product
you can see my route
Auth::routes();
Route::prefix('products')->group(function () {
Route::get('/', [ProductsController::class, 'index']);
Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
});
On this below route giving the product_view page which I can't understand why is giving
Route::get('/test_route', , function () {
return 'Hello World';
});
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| Domain | Method | URI | Name | Action
| Middleware |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| | GET|HEAD | api/user | generated::suiIeX6oHZWSnSP1 | Closure
| api |
| | | | |
| App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | sanctum/csrf-cookie | generated::WKOquUKjM8nfHvKl | Laravel\Sanctum\Http\Controllers\CsrfCookieController#s
how | web |
| | GET|HEAD | products | generated::IucNuoXqqYtZPi70 | App\Http\Controllers\ProductsController#index
| web | |
| | GET|HEAD | {code} | products.view_product | App\Http\Controllers\ProductsController#view_product
| web |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
I have a blade template where I call a route to the show method:
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>
That route is controlled by a resource controller and the routes are set like this:
Route::resource('profile', 'App\Http\Controllers\ProfileController')->middleware('auth');
All the routes of that controller are defined as you can see from the output of my php artisan route:list:
| | GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController#index | web |
| | | | | | auth |
| | POST | profile | profile.store | App\Http\Controllers\ProfileController#store | web |
| | | | | | auth |
| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController#create | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController#show | web |
| | | | | | auth |
| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController#destroy | web |
| | | | | | auth |
| | PUT|PATCH | profile/{profile} | profile.update | App\Http\Controllers\ProfileController#update | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController#edit | web |
| | | | | | auth |
So far the controller only has a dd function since I haven't started writing it yet and the show method is the only one with anything written yet:
public function show($id)
{ dd($id);}
But for some reason, whenever I try acessing that route, it returns a 404 Not Found error. I tried accessing the index method and it works, but the show method always returns the 404 Not Found error. I also tried php artisan route:clear but it didn't solve the problem. What could I be doing wrong here?
Try out it , list all of router
php artisan route:list
Or you can as such that
Route::resource('profile', 'ProfileController', [
'names' => [
'index' => 'show.show'
'store' => 'profile.new',
]
]);
And if you try this ?
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>
Route::group(['prefix'=>'cart'], function (){
Route::get('', 'CartController#index')->name('cart.index');
Route::get('{id}', 'CartController#create')->name('cart.create')->middleware('auth');
Route::any('update/{id}/{qty}', 'CartController#update')->name('cart.update')->middleware('auth');
Route::any('saveorder', 'CartController#store')->name('cart.store')->middleware('auth');
Route::any('delete/{rowId}', 'CartController#destroy')->name('item.delete')->middleware('auth');
Route::any('empty', 'CartController#empty')->name('cart.empty')->middleware('auth');
});
Some of the routes still works but some of them are broken and returning 404 even that the routes exist
the broken routes are : item.delete , cart.empty , cart.store
| auth:api |
| | GET|HEAD | cart | cart.index | App\Http\Controllers\CartController#index | web |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | cart/delete/{rowId} | item.delete | App\Http\Controllers\CartController#destroy | web |
| | | | | | auth |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | cart/empty | cart.empty | App\Http\Controllers\CartController#empty | web |
| | | | | | auth |
| | POST | cart/saveorder | cart.store | App\Http\Controllers\CartController#store | web |
| | | | | | auth |
| | PUT | cart/update/{id}/{qty} | cart.update | App\Http\Controllers\CartController#update | web |
| | | | | | auth |
| | GET|HEAD | cart/{id} | cart.create | App\Http\Controllers\CartController#create | web |
| | | | |
if you're using a GET method with cart.empty and cart.store then they will be handled by cart.create, you should put those routes first (order matters), also its better to be explicit with the route methods instead of using Route::any
Route::prefix('cart')->group(function () {
Route::get('', 'CartController#index')->name('cart.index');
Route::any('saveorder', 'CartController#store')->name('cart.store');
Route::any('empty', 'CartController#empty')->name('cart.empty');
Route::any('delete/{rowId}', 'CartController#destroy')->name('item.delete');
Route::any('update/{id}/{qty}', 'CartController#update')->name('cart.update');
Route::get('{id}', 'CartController#create')->name('cart.create');
});
Route::group(['prefix' => 'cart', 'middleware' => ['auth']], function(){
Route::get('/', 'CartController#index')->name('cart.index');
Route::get('/{id}', 'CartController#create')->name('cart.create');
Route::any('/update/{id}/{qty}', 'CartController#update')->name('cart.update');
Route::any('/saveorder', 'CartController#store')->name('cart.store');
Route::any('/delete/{rowId}', 'CartController#destroy')->name('item.delete');
Route::any('/empty', 'CartController#empty')->name('cart.empty');
});
Route::prefix('cart')->group(function (){
Route::middleware(['auth'])->group(function(){
Route::get('{id}', 'CartController#create')->name('cart.create');
Route::put('update/{id}/{qty}', 'CartController#update')->name('cart.update');
Route::post('saveorder', 'CartController#store')->name('cart.store');
Route::delete('delete/{rowId}', 'CartController#destroy')->name('item.delete');
Route::post('empty', 'CartController#empty')->name('cart.empty');
});
Route::get('/', 'CartController#index')->name('cart.index');
});
Two things to consider. Your cart.index could potentially match URI's underneath the route. For security, please try not to use Route::any if possible. As explained in https://laravel.com/docs/7.x/routing. As #Sobir suggests, run php artisan route:list and see your actual list of routes as Laravel sees it.
If something is missing or ambiguous, you may have to reconsider some route parameters to avoid confusion. Or regroup them in different ways. Your route list will certainly grow. Anticipate on what you're planning in the future of what you are building.
So I got a template of a Flutter app that retrieves all its data from a website using HTTP get requests.
I have the following method that gets the list of resturaunts:
Future<Stream<Restaurant>> getNearRestaurants(LocationData myLocation, LocationData areaLocation) async {
String _nearParams = '';
String _orderLimitParam = '';
if (myLocation != null && areaLocation != null) {
_orderLimitParam = 'orderBy=area&limit=5';
_nearParams = '&myLon=${myLocation.longitude}&myLat=${myLocation.latitude}&areaLon=${areaLocation.longitude}&areaLat=${areaLocation.latitude}';
}
final String url = '${GlobalConfiguration().getString('api_base_url')}restaurants?$_nearParams&$_orderLimitParam';
final client = new http.Client();
final streamedRest = await client.send(http.Request('get', Uri.parse(url)));
return streamedRest.stream.transform(utf8.decoder).transform(json.decoder).map((data) => Helper.getData(data)).expand((data) => (data as List)).map((data) {
return Restaurant.fromJSON(data);
});
}
However when I swap the template's url variable for my own website, the app gets stuck and streamRest returns with an error 404 page.
Tried Solutions:
I surrounded it with a try/catch block and it gave me no exceptions.
I also installed postman and checked my website with the GET statement for the same list of restaurants I try to retrieve in the flutter code posted above and see this: Postman GET screenshot
Its as if my website cannot route to the specific pages in my API folder. But they are all defined in api.php.
Update 1:
My web.php looks like this https://pastebin.com/QRG300uL. It seems to be similar to what was suggested below
Update 2:
I ran php artisan route::list and it showed that all the routes seem to be there:
| | POST | api/restaurant_reviews | restaurant_reviews.store | App\Http\Controllers\API\RestaurantReviewAPIController#store | api |
| | GET|HEAD | api/restaurant_reviews | restaurant_reviews.index | App\Http\Controllers\API\RestaurantReviewAPIController#index | api |
| | GET|HEAD | api/restaurant_reviews/create | restaurant_reviews.create | App\Http\Controllers\API\RestaurantReviewAPIController#create | api |
| | DELETE | api/restaurant_reviews/{restaurant_review} | restaurant_reviews.destroy | App\Http\Controllers\API\RestaurantReviewAPIController#destroy | api |
| | GET|HEAD | api/restaurant_reviews/{restaurant_review} | restaurant_reviews.show | App\Http\Controllers\API\RestaurantReviewAPIController#show | api |
| | PUT|PATCH | api/restaurant_reviews/{restaurant_review} | restaurant_reviews.update | App\Http\Controllers\API\RestaurantReviewAPIController#update | api |
| | GET|HEAD | api/restaurant_reviews/{restaurant_review}/edit | restaurant_reviews.edit | App\Http\Controllers\API\RestaurantReviewAPIController#edit | api |
| | GET|HEAD | api/restaurants | restaurants.index | App\Http\Controllers\API\RestaurantAPIController#index | api |
| | POST | api/restaurants | restaurants.store | App\Http\Controllers\API\RestaurantAPIController#store | api |
| | GET|HEAD | api/restaurants/create | restaurants.create | App\Http\Controllers\API\RestaurantAPIController#create | api |
| | GET|HEAD | api/restaurants/{restaurant} | restaurants.show | App\Http\Controllers\API\RestaurantAPIController#show | api |
| | DELETE | api/restaurants/{restaurant} | restaurants.destroy | App\Http\Controllers\API\RestaurantAPIController#destroy | api |
| | PUT|PATCH | api/restaurants/{restaurant} | restaurants.update | App\Http\Controllers\API\RestaurantAPIController#update | api |
| | GET|HEAD | api/restaurants/{restaurant}/edit | restaurants.edit | App\Http\Controllers\API\RestaurantAPIController#edit | api |
| | POST | api/send_reset_link_email | | App\Http\Controllers\API\UserAPIController#sendResetLinkEmail | api |
| | GET|HEAD | api/settings | | App\Http\Controllers\API\UserAPIController#settings | api |
Solution:
This worked for me after changing alot of things, I changed my GET request url from "www.domain.com/api/resturants" to "www.domain.com/public/api/resturants"
Well i don't know about your flutter code for i use different methods in retrieving data from api but about the routes i suggest you do like me
in web.php the route file
//Api routes
Route::get('/company/api/fetch', 'ApiController#fetch_companies');
my api controller
public function fetch_companies()
{
$companies = Companies::all();
return response()->json($companies);
}
this way you will get the data passed to the route /company/api/fetch (you can modify that as you want) and when a get request enter this page it will return json
and for the request handling in flutter side i suggest you make your functions and classes as it is in the documentations
Note: that the flutter solution that i suggested may not work with your case for you are using Stream which is different than this type of requests because this type runs only ones while the Stream runs many times and gets data every time it gets new data from the server
I'm creating an ajax request to get item details
Here's what my controller method looks like.
class SystemItemsController extends Controller
{
function getDetails(Request $request){
$response = SystemItems::where('item_name', 'like', '%' .$name . '%')->get();
return response()->json($response,200);
}
}
and my
routing name
Route::get("/system-items/item-details","SystemItemsController#getStockDetails");
question : what would be the best naming convention for my route(item-details) and method(getStockDetails)?
follow up Q : can i do this using laravel resource?
You can use kebab-case and plural in the URI pattern, but camelCase and singular for Controller name, as that is what Laravel will look for if trying to do route–model binding.
You can use it for resouce routes, but note, for this route
Route::resource('item-details', 'ItemDetailController');
the route parameter will result in snake_case and singular
/item-details/{item_detail}
For the controller methods the conventional names are index, show, create, store, edit, update and delete. And snakeCase for custom methods.
Also you can add a route group to prefix with some uri like /system-items
Route::group(['prefix' => 'system-items'], function () {
Route::resource('item-details', 'ItemDetailController');
});
run php artisan route:list to see the result
+--------+-----------+-----------------------------------------------------+-------------------------+---------------------------------------------------------------------------+--------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-----------------------------------------------------+-------------------------
| | GET|HEAD | api/v1/system-items/item-details | item-details.index | App\Http\Controllers\Api\v1\ItemDetailController#index | api |
| | POST | api/v1/system-items/item-details | item-details.store | App\Http\Controllers\Api\v1\ItemDetailController#store | api |
| | GET|HEAD | api/v1/system-items/item-details/create | item-details.create | App\Http\Controllers\Api\v1\ItemDetailController#create | api |
| | GET|HEAD | api/v1/system-items/item-details/{item_detail} | item-details.show | App\Http\Controllers\Api\v1\ItemDetailController#show | api |
| | PUT|PATCH | api/v1/system-items/item-details/{item_detail} | item-details.update | App\Http\Controllers\Api\v1\ItemDetailController#update | api |
| | DELETE | api/v1/system-items/item-details/{item_detail} | item-details.destroy | App\Http\Controllers\Api\v1\ItemDetailController#destroy | api |
| | GET|HEAD | api/v1/system-items/item-details/{item_detail}/edit | item-details.edit | App\Http\Controllers\Api\v1\ItemDetailController#edit
Of course, all of these are conventions and you can customize everything by doing it by hand and using your own conventions.