Unable to access laravel 5.5 API route using Postman software - php

My api.php route is
Route::get('/allposts','PostController#index');
Controller function is and working with web.php Route file
public function index()
{
$posts= Post::all();
return PostResource::collection($posts);
}
my Resource is toArray function is
public function toArray($request)
{
return parent::toArray($request);
}
Postman link using GET are
"queuetest.com/api/allposts"
"http://queuetest.com/api/allposts"
both are not working
and getting Result in both Post man and browser: Sorry the page your looking for could not be found

Do you get something other than the Sorry the page you're looking for could not be found message when you do var_dump('test');die(); in your index method as the first line.
Edit: try to remove then / before /allPosts
Another edit: check in your RouteServiceProvider if the mapApiRoutes prefix is set to 'api'

If you are using the default route configuration that comes out of the box, you should place your route in routes/api.php if you want to access it in the /api namespace. Right now, the route should be accessible from http://queuetest.com/allposts.
This behavior can be configured in App\Providers\RouteServiceProviders.php. If you have a modified route configuration, there are numerous things that could cause this behaviour, and it is impossible to locate the problem without seeing more code.

Related

Laravel 5.4 function index try using function show

I have route:
Route::resource('admin/question', 'QuestionsController');
and function index:
public function index() {
return "Hello";
}
But when I try used index Laravel returned me the error:
Method [show] does not exist.
I'm using the link:
http://localhost:8012/siwz/siwz/public/admin/question
The server is WampServer program.
I can only use index function when I change route file:
Route::get('admin/question/index', 'QuestionsController#index');
Route::resource('admin/question', 'QuestionsController');
In Laravel version 5.3 I did not have to do it, it was enough to use:
Route::resource('.../...', '...Controller');
Actually, the URL is going to the correct function. admin/question should go to index. admin/question/{question} is the route that goes to show.
Take a look here and check how Laravel create Resource routes:
https://laravel.com/docs/5.4/controllers#resource-controllers
Since that you didn't provide your full routes. I am guessing that the link you were accessing is going to the wrong controller. You should check the ordering of the routes. Maybe you are accessing something like this on your route,
Route::resource('admin','AdminController');
And the AdminController doesn't have a method show(). Thats why laravel return that error.
Here's you can do
Comment out the rest of routes except route that you are accessing.
Try to reorder the affected routes. Maybe Laravel is confused with your route.

NotFoundHttpException in RouteCollection when routing with additional controller in laravel 5.2

I got this error-->'NotFoundHttpException in RouteCollection.php line 161'..When i try to call my additional controller in laravel 5.2..Already I did php artisan serve to activate localhost:8000..can you please explain the basic layout of routing with controller in laravel?
NotFoundHttpException occurs when no given route is matched to your given request to a certain endpoint/url.
Make sure you are sending the request to the correct url which is correctly defined in your routes.php (web.php for laravel 5.3+) with it's correct verb, (GET, POST, PATCH, etc).
Basic flow goes like this:
In your routes.php, you'd define a route like:
Route::get("/users", "UsersController#show");
then in your Http folder define that given controller with it's name which you referred in above call and anything proceeding # symbol is a callback function which gets called automatically.
So in your http/UsersController.php, you'd have:
public function show(Request $request) {
//Do something with your request.
return "Something"; //could be an array or string or
//whatever since laravel automatically casts it into JSON,
//but it's strongly recommended to use transformers and compact method.
}
For more information try looking at laravel docs, they provide an amazing way to get started tutorial. Laravel Docs

Laravel 5 api route

Brief:
Actually, I'm little bit confused while using Laravel api route file.
Question:
If I need to access the data of my web site in other platform (like android app) that is made using laravel, then should I create a different route in api.php file?
If yes then I will be declaring two routes and controllers for each request, first in web.php and second in api.php. Is it correct?
Basically, I want to ask that how I can make an api, so that I can access the data in website as well as in other platforms?
I was searching for a good tutorial for this, but I didn't got a good one.
Ideally the API routes and Web routes should be completely different but if you want it to be same then instead of defining routes in different file you can add routes only in web.php and add a special parameter from your client and in controller if you are getting the parameter then return the JSON Object or else return the view.
Eg.
web.php
Route::get('getUsers','UserController#getUsers');
UserController.php
...
public function getUsers(Request $request)
{
...
if ($request->has('api')) {
return $users; //API Route (Laravel will by Default return the JSON Response no need to do json_encode)
}
return view('pages.user_list'); //Normal Routes hence returning View
}
...
Requests
Normal Request
<Yourdomain>/getUsers
API Request
<Yourdomain>/getUsers?api=true
I hope that helped...
Write your api routes in api.php and web routes in web.php.
The Api routes always have the name api in the routes thus you can differentiate the routes., I mentioned here because as #Akshay Khale mentioned an example with query parameter.
if you want to use the same controller for both API and Web, Api Requests always have the Header Content-Type : Json and "Accept":"application/json" so in your controller you can do it as below.
public function getUsers(Request $request)
{
...
if ($request->wantsJson()) {
return response()->json($users, 200); //here why we are extending response object because using json() method you can send the status code with the response.
}
return view('pages.user_list'); //Normal Routes hence returning View
}
for the laravel 5.6 and above the above answers won't work for me , so here is my 2 cents.
I have put the routes in web.php and api.php and normal no any magic tricks .
public function getUsers(Request $request)
{
....
if( $request->is('api/*')){
...
return response()->json($user_data, 200);
}
...
return view('users', ['users_data'=>$user_data]);
}
It will return json output for
127.0.0.1:8000/api/users
and normal view in html for
127.0.0.1:8000/users

laravel asks for routes though the route exist

I have RestaurantController with these methods:
save
show($id)
when I finish executing the save method, I want to redirect the user to the show($id) method.
I tried this:
return Redirect::route('show', array($restaurant->id));
but I got :
InvalidArgumentException
Route [/show] not defined.
I also tried this:
NotFoundHttpException
though the show method exists.
in my routes.php I have:
Route::resource('restaurants', 'RestaurantsController');
could you help please?
Route is the name of the route, so in this instance it'd be:
return Redirect::route('restaurants.show', [$restaurant->id]);
See here for more information regarding redirects.
Also, just fyi, from the command line you can run php artisan routes to see a full list of routes and their corresponding names.

laravel giving incomprehensible(to me) http exception

I use laravel 4 and am not able to show a component :
this is a line from my routes
Route::resource('/', 'PostsController');
and this is my show function from PostsController.php
public function show($id) {
return "HI";
}
And This is the line that links to the function from my view
<h1>{{$post['title']}}</h1>
And It properly links to localhost:8000/show/1
But I'm amazingly getting a Not found HTTP exception from laravel.
How do I get this to work?
So the answer is that #NihalSahu is wrong (me) and that resourceful routing doesn't work like that it would be infinitely better so as to set the router to host/{id}

Categories