Target class [Frontend\PagesController] does not exist - php

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;

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 [Admin\AdminController] does not exist

I'm developing a website using Laravel 8 by watching youtube tutorial, and I encounter an error message and don't know how to solve it. I tried to change the namespace but it didn't work.
ERROR MESSAGE PICT
My AdminController.php code
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function dashboard(){
return view('admin.admin_dashboard');
}
}
And my 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('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::prefix('/admin')-> namespace ('Admin')-> group (function(){
//All the admin roles will be defined here
Route::get('dashboard','AdminController#dashboard');
});
can anyone help me to solve this? thanks.
Your namespace should be full path
Route::prefix('/admin')-> namespace ('Admin')-> group (function(){
...
});
Please change as below
Route::prefix('/admin')->namespace('App\\Http\\Controllers\\Admin')-> group (function(){
...
});
You have to include the whole path in namespace, like this...

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

Why am I getting this error: "class app\Laptop not found?"

I am trying different methods to resolve it for 5 hours but can't may any method work.
This is my web.php file:
<?php
/*
|--------------------------------------------------------------------------
| 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('/', 'LaptopController#show');
This is Laptop.php(Model):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Laptop extends Model
{
//
}
This is LaptopController.php (Controller):
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use app\Laptop;
class LaptopController extends Controller
{
public function show(){
$laptop=Laptop::all();
echo $laptop->name;
}
}
Inside the controller you are using
use app\Laptop;
but instead it should be
use App\Laptop;
In addition to this there is the problem that you are trying to access name of a Collection: if you want just the first one you should do Laptop::first()

API Routing Laravel 5.5

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'));
}

Categories