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

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.

Related

Route returning Error 404 but the route exists

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>

SOLVED Laravel website returns "Error 404" for GET requests

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

Laravel best naming convention for controller method and route

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.

Laravel 5.1 - Routes Error - NotFoundHttpException in RouteCollection.php line 143:

This error happen in my laravel 5.1 project, when trying to access http://localhost:8000/dashboard.user.update after I click Submit on my edit form.
NotFoundHttpException in RouteCollection.php line 143:
This error happen when I run update function in my controller. I have no error when runing other function like index or show.
This is my routes:
...
// Dashboard routes...
Route::resource('dashboard/user', 'UserController');
Route::resource('dashboard', 'DashboardController');
...
I already runs php artisan route:list to see if my routes are working:
+--------+----------+-----------------------+------------------------+---------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+-----------------------+------------------------+---------------------------------------------+------------+
...
| | POST | dashboard/user | dashboard.user.store | App\Http\Controllers\UserController#store | auth |
| | GET|HEAD | dashboard/user | dashboard.user.index | App\Http\Controllers\UserController#index | auth |
| | GET|HEAD | dashboard/user/create | dashboard.user.create | App\Http\Controllers\UserController#create | auth |
| | DELETE | dashboard/user/{user} | dashboard.user.destroy | App\Http\Controllers\UserController#destroy | auth |
| | GET|HEAD | dashboard/user/{user} | dashboard.user.show | App\Http\Controllers\UserController#show | auth |
| | PUT | dashboard/user/{user} | dashboard.user.update | App\Http\Controllers\UserController#update | auth |
| | PATCH | dashboard/user/{user} | | App\Http\Controllers\UserController#update | auth |
...
Now I don't know what to do, can help?
Like your routes table says you need PUT or PATCH for an update call. Your route is correct but laravel checks the routes also for the method type.
I think you want to display a view and not return a json (RestAPI) if so than the implicit controllers are much better than resources!
http://laravel.com/docs/5.1/controllers#implicit-controllers
If you want to use them you have to rename your methods to getIndex(), getEdit(), postStore() and if you want post not put than postUpdate().
If it's an update, tell laravel it's a "PUT" request. Since simply POST method for that url with the user_id not exists.
<input name="_method" type="hidden" value="PUT">
Something like this:
<form method="POST" action="http://localhost:8000/dashboard/user/1">
<input name="_method" type="hidden" value="PUT">
//...

Laravel not able to route to controller

Here is my controller class:
class CallrequestController extends BaseController {
public function getIndex()
{
return View::make('results');
}
}
which is saved in:
--app
--controllers
--CallrequestController.php
then I autoloaded via composer:
composer dump-autoload
then i add the route to route file:
Route::controller('getrequest','CallrequestController');
here is the registered route via php artisan routes:
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
| | GET|HEAD / | | Closure | | |
| | GET|HEAD callrequest/index/{one?}/{two?}/{three?}/{four?}/{five?} | | CallrequestController#getIndex | | |
| | GET|HEAD callrequest | | CallrequestController#getIndex | | |
| | GET|HEAD|POST|PUT|PATCH|DELETE callrequest/{_missing} | | CallrequestController#missingMethod | | |
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
yet its not routing to the controller, i get the default "Whoops, looks like something went wrong." error message.
what am i doing wrong?
edit also here is a sample url:
mydomainname.com/getrequest

Resources