API Routing Laravel 5.5 - php

I have basic controller, where I want it to receive any json request. Am new to api routing. I get Sorry No Page Found When I use POST MAN. First I tested it on GET and made it call a simple return but throws the error."Sorry, the page you are looking for could not be found."
I removed the api prefix in the RouteServiceProvider.php and to no success.I put my demo controller
Routing api.php
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::get('/test_api/v1', 'TestController#formCheck');
TestController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function formCheck(){
return "YES !!!";
}
public function formPost(Request $request)
{
$formData = $request->all();
return response()->json($formData, 201);
}
}

In the app/Providers/RouteServiceProvider.php.
Remove prefix('api') & it shuld look like this.
protected function mapApiRoutes()
{
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}

Related

Laravel Controller does not exists

Im starting to programming in Laravel and trying to understood how the routes works. But it always said that the class "UserControler" that I just created it doesn't exist and I don't know why.
Routes > web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/user',[UserController::class, 'index']);
I have this in the controllers directory that I just made with the artisan.
app>http>controller>UserController.php
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
public function index()
{
return "Hello World!";
}
}
I get this error:
BindingResolutionException
PHP 8.1.4
9.9.0
Target class [UserController] does not exist.
You forgot to import the controller to your route files. Currently your web.php file can't resolve UserController class, because it doesn't know what it is. You can import it using the use keyword:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController; //This line
just add your class namespace lik:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/user',[UserController::class, 'index']);
what zlatan posted was exactly what i forgot too.... It's so obvious to declare it that i didn't see it anymore...
so make sure you declare use App\Http\Controllers\UserController; in the web.php file.

Target class [Frontend\PagesController] does not exist

I know another way to declare 'App\Http\Controllers\Frontend\PagesController#blog' like that but what is the wrong when I use 'Frontend\PagesController#blog'?
Note: I am using Laravel 8.
This is route:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/blog', 'Frontend\PagesController#blog')->name('blog.page');
This is controller:
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function blog(){
return view('frontend.pages.blog');
}
}
You need to define the namespace for the controllers either in the RouteServiceProvider.php or in web.php.
In app/Providers/RouteServiceProvider.php
protected $namespace = 'App\Http\Controllers';
In routes/web.php
use App\Http\Controllers;

Call to undefined function App\Http\Controllers\veiw() error in Laravel

I have a problem in my starting codes in Laravel,
it's my error :
Error
Call to undefined function App\Http\Controllers\veiw()
it's my web.php (routes page) :
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/users', 'UserController#user');
and it's my Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function user() {
return veiw('index');
}
}
Hope you help me ...
You mistyped view: It is view, not veiw

Laravel phpunit api return 405

I can not test the api with phpunit testing. I have created a testcase and a test route. But it gives me an error like Expected status code 200 but received 405.
My User controller test:-
<?php
namespace Tests\Feature\API\Medical\V1;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\User;
class UserControllerTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testLogin()
{
$user = User::find(1);
$response = $this->actingAs($user,'api')->json('POST','/api/test');
$response->assertStatus(200);
}
}
I have not done anything to testCase base class.
My api routes:-
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('/test',function(){
return response()->json([
'success'=>true
]);
});
But this route is working on postman. When i change http method to GET it gives me my default route on routes/web.php. How to get my actual json to my testcase? thanks in advance.
I faced the same problem.
The project was raised in docker. In the .env file, you need to write:
APP_URL=http://localhost

"Target [App\Http\Controllers\Controller] is not instantiable."

I'm trying to follow laracasts tutorial on laravel fundamentals but after getting composer and laravel installed with no problems I can't get my routes file to work with the controller I've reinstalled laravel copied it exactly how laracasts has his but still nothing, anyone see anything wrong with these two files?
routes.php files
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', 'Controller#index');
Route::get('contact', 'Controller#contact');
controller.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
abstract class Controller extends BaseController
{
use DispatchesJobs, ValidatesRequests;
public function ___construct()
{
$this->middleware('guest');
}
public function index()
{
return 'hello world!';
}
public function contact()
{
return 'Contact me!';
}
}
I have it hosted on localhost:8888 using phps server command if that is any help.
The reason might be that your controller class is abstract, hence it's not instantiable. Remove the abstract keyword.
if your route is get or post and actually you have implemented resource, so you get this type of error

Categories