Laravel API Not Found - php

I'm trying to test my api and for this matter I don't need authentication for my api all I want to do is to share my published posts with api but I get 404 page.
Code
controller
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Post;
class PostController extends Controller
{
public function index(){
return Post::orderby('id', 'desc')->where('status', '=', '1')->get();
}
public function single($slug){
return Post::where('slug', $slug)->where('status', '=', '1')->firstOrFail();
}
}
api.php (routes folder)
Route::get('posts', 'API\PostController#index');
Route::get('posts/{slug}', 'API\PostController#single');
I tried to access my api posts with url like: http://newapp.test/api/posts and it returns 404 error.
Any idea?
Update
api.php
<?php
use Illuminate\Http\Request;
// Route::middleware('auth:api')->get('/user', function (Request $request) {
// return $request->user();
// });
Route::get('posts', 'API\PostController#index');
Route::get('posts/{slug}', 'API\PostController#single');

Leave all things as it is and RUN Command
php artisan route:clear

Run command php artisan route:list. It will show you list of available routes in your application. In this way, you could first verify the existing routes and the ones you are trying to access.

My API mistake was: redirecting back
if($validator->fails()){ return redirect()->back()->withInput()->with('error',$validator->errors()->first());}
corrected by: returning a JSON
if($validator->fails()){ return $this->responseWithError($validator->errors()->first()); }
responseWithError is a helper method that returns JSON in a certain structure

Related

Target class [PageController] does not exist, in laravel

I have created an api in api.php, But in git bash I am getting the following issue :
My codes from api.php :
<?PHP
use Illuminate\Http\Request;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('summitRegistration',[PageController::class,'summitRegistration']);
my controller path is :
Your controller is under User namespace, so you should change the line
Route::post('summitRegistration',[PageController::class,'summitRegistration']);
to
Route::post('summitRegistration',[User\PageController::class,'summitRegistration']);
Route::post('summitRegistration',[PageController::class,'summitRegistration']);
To
Route::post('summitRegistration',[\App\Http\Controllers\User\PageController::class,'summitRegistration'])->name('summitRegistration');

Laravel 8 Jetstream custom api

I'm attempting to create a login endpoint so that I can use it without a blade.php file. While working with Unreal engine I cant use a actual web page so wanting to create a Login endpoint that will send back custom json. The below is what I have set up. When i use postman or Python requests I get a page response of Not Found or page expired. How can I get this to return the test or failed?
api.php
Route::post('/unreal-login', 'App\Http\Controllers\UnrealLoginController#authenticate');
UnrealLoginController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class UnrealLoginController extends Controller{
public function authenticate(Request $request){
// Retrive Input
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
// if success login
return "test";
//return redirect()->intended('/details');
}
// if failed login
return "failed";
}
}

Showing wrong view in laravel 5.4

I am using laravel 5.4 and trying to get index page, i am using following routes
Route::get('/',
['as' => 'home_page',
'uses' => 'Controller#index']);
and index function in controller looks like this:
public function index()
{
return view('index');
}
But when I visit mydomain.com, I get a different view than index.blade.php.
and it is fine when I use mydomain.com/? or on my local server.
I have searched everywhere in my code and in a google, but didn't found anything, any help?
ie: let me know if any further information required.
First make sure you are calling the right controller, and this dont have a specific middleware blocking the acess to your index method and index.blade.php is inside view folder.
If all of this is fine try this code on your rotes file:
Route::get('', function () {
return view('index');
})
Try this.
First use the make:controller Artisan command to create a controller file. let's say it is homeController.
php artisan make:controller homeController
Then in the homeController file write your code to get the view.
<?php
namespace App\Http\Controllers;
class homeController extends Controller
{
public function index()
{
return view('index');
}
}
Then define a route to this controller.
Route::get('/', 'homeController#index');
For more information please refer https://laravel.com/docs/5.5/controllers
There was a cached view saved on my server, I used
php artisan cache:clear and it got fixed. Thank you everyone for the support.

POST request from Postman to Laravel

I am trying to send a post request to a Laravel project using Postman, but I get a "419 unknown status" response
routes\web.php:
Route::post('/myaction', 'MymodelController#myaction');
app\Http\Controllers\MymodelController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mymodel;
class MymodelController extends Controller
{
function myaction()
{
return redirect('/');
}
}
Why does this happen?
The same error appears independently of the content of myaction()
As you are requesting api you should write your route in api.php instead web.php
web.php require _token the csrf field
By default, Laravel use the middleware VerifyCsrfToken.
See this
for more details.
You need to add your URL to the $excludes field inside VerifyCsrfToken class.
Do you have defined route for redirect('/'); in web.php ?

Laravel: redirecting to a controller#store method redirects to the index instead

I am trying to redirect to PostsController#store but the page redirects to PostsController#index:
In routes.php:
Route::Resource('posts', 'PostsController');
Route::Resource('reviews', 'ReviewsController');
ReviewsController
class ReviewsController extends Controller {
public function store(Request $request) {
// (do a bunch of stuff)
return redirect(action('PostsController#store',[$request]));
}
}
PostsController
class PostsController extends Controller {
public function index() {
dd('Incorrectly redirects here');
}
public function store(Request $request, Post $post) {
dd('This is where I am trying to redirect to');
}
}
No error or exception occurs. However, once redirected, the url is:
http://localhost/laravel2devel/public/posts?POST%20/laravel2devel/public/reviews%20HTTP/1.1%0D%0AAccept:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8%0D%0AAccept-Encoding:%20%20%20%20%20%20%20%20%20%20%20gzip,%20deflate%0D%0AAccept-Language:%20%20%20%20%20%20%20%20%20%20%20en-US,en;q=0.8%0D%0ACache-Control:%20%20%20%20%20%20%20%20%20%20%20%20%20max-age=0%0D%0AConnection:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20keep-alive%0D%0AContent-Length:%20%20%20%20%20%20%20%20%20%20%20%20132%0D%0AContent-Type:%20%20%20%20%20%20%20%20%20%20%20%20%20%20application/x-www-form-urlencoded%0D%0ACookie:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20XSRF-TOKEN=eyJpdiI6InhzN2JiK0FvYnIrUnVoeGZkcGNHOXc9PSIsInZhbHVlIjoiMFdrdnluNTdrR3l3OTlrZE9QSDA1WVwvalNoeDhHbG0wUXlvT0NBblRyWDNocFwvMExCZ0dqdVppbjR2M29SdnRmbWRDMkdRc042XC9ib3hrd2xZa1JCTmc9PSIsIm1hYyI6IjQ0ZmM4YTg4YmIxNTliMGY0MzI0OGMxMjMyZGM0ZDU4ZGM4MTVlMGM2NzVmZWNmM2YzZjI5YWU4OTJhOWM5MGYifQ%3D%3D;%20laravel_session=eyJpdiI6IjRCRThcLzdaR3ZaUUZJdTVQcmtVZk5BPT0iLCJ2YWx1ZSI6IlJYTlFaWk5WZEZQbHdkQzhtalhGbko0dnUzdzN0UjhOM2FLZUJQMkloNkMwdGRjc3VcL3lNUjBaXC9acktrUkxvRzZEWW4rVlY3Q0o2alB3VnJnNEZDdnc9PSIsIm1hYyI6ImIwMjRlMzIzYWYxNDI5NjEzYmFmZjljMjg4MmFkYzU1MTkzZDVkOWRjM2IwMzZlNTM1MTE2ZWExYTA3NmYyNzgifQ%3D%3D%0D%0AHost:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20localhost%0D%0AOrigin:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://localhost%0D%0AReferer:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20http://localhost/laravel2devel/public/swords/1%0D%0AUpgrade-Insecure-Requests:%201%0D%0AUser-Agent:%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Mozilla/5.0%20%28Windows%20NT%206.1;%20WOW64%29%20AppleWebKit/537.36%20%28KHTML,%20like%20Gecko%29%20Chrome/48.0.2564.97%20Safari/537.36%0D%0A%0D%0A_token=DeQeCCA8e19IIsxGhouybXiqIFoAMLsQ6sgp5EMF&post_title=asdfdas&user_id=2&reviewable_id=1&reviewable_type=item&post_body=asdfdfas
If you use resource controller then, store() method is expected to be called with HTTP POST. If you redirect then it will use GET, so Laravel will call index() instead. See Laravel HTTP Resource Controllers documentation.
When you add a route as a resource it will handle the Restful requests to each methods.
A get request to the url will execute the index() method. For the store() method to execute a post request need to be given. store() method is to add the resource.
Please go through the resource controller documentation.
Im not clear about your question but my idea is if you wants store reviews for particular post just use your routes like this
Route::Resource('posts', 'PostsController');
Route::Resource('posts.reviews', 'ReviewsController');
then check the route:list then you will get the list of routes, i hope you have two tables then use foreign key you can archive your goal
i hope this is good tutorial for you LINK

Categories