Laravel : Class controller does not exist - laravel

I have created a simple controller and define a function. But when i run this it returns an error that controller does not exist.
In my web.php assign a route.
<?php
Route::get('/', function () { return view('front.welcome'); });
Route::get('plan','PlanController#PlanActivity')->name('plan');
On otherside in my controller my code:
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
class PlanController extends Controller {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}
This controller created on App\Http\Controllers\Front - on front folder
Error :
ReflectionException (-1)
Class App\Http\Controllers\PlanController does not exist

Add Front part to:
Route::get('plan', 'Front\PlanController#PlanActivity')->name('plan');
Also, change the top of the controller to:
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
And run composer du.
From the docs:
By default, the RouteServiceProvider includes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace.

First when defining route, make sure to use the correct path for the controller. the correct is:
Route::get('plan','Front/PlanController#PlanActivity')->name('plan');
Second you have imported Controller Class as BaseController. so you should extends BaseController not Controller:
class PlanController extends BaseController {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}

Related

I am getting target class does not exist error in laravel 8

The routing process has changed in Laravel version 8. I did as in the internet but it gives an error. Where am I doing wrong?
Route file
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Backend\BackendHomeController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/admin', [BackendHomeController::class, 'index'])->name("index");
Controller file
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public static function index()
{
return view("backend.index");
}
}
BackendHomeController also like this. Everything seems to be correct, but am I doing something wrong with using?
Error says
target class does not exist error
Class is referenced as BackendHomeController but your file is named HomeController. These should align for autoloading to be working.

Laravel 8 subfolder Controller

I created folder in Controller. But Laravel is giving
Target class [App\Http\Controllers\SAIDE\SaideMerchantController] does not exist.
error
-->Controller
<?php
namespace App\Http\Controllers\SAIDE;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
class SaideMerchantController extends Controller
{
public function home()
{
return view('backoffice.saide.maindashboard');
}
Route::get('/maindashboard','SAIDE\SaideMerchantController#home');
}
-->end controller
How to use controller in Folder ?
We don't see a complete route file, but try the change the get method to
Rouge::get('/mainboard', [App\Http\Controllers\SAID\SaidMerchantController::class, 'home']);
Check out Laravel documentation for version 8 routing
https://laravel.com/docs/8.x/routing#basic-routing

How to Solve Controller Error in Laravel6?

I am getting AdminController error, and i am unable to solve this. I am using namespace in web files. Please let me know where i am mistaking.
My error is this..
Target class [App\Http\Controllers\Admin\AdminController] does not exist.
Here are my web.php file
Route::namespace('Admin')->prefix('admin')->name('admin.')->middleware('can:manage-users')->group(function(){
Route::resource('/users', 'UsersController',['except'=>['show','create','store']]);
Route::resource('/dashboard', 'AdminController');
Route::resource('blog', 'BlogController');
});
Here are my AdminController.php File..
<?php
namespace App\Http\Controllers;
use App\Admin;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index()
{
return view('admin.index');
}
}
The error caused by namespace :
Route::namespace('Admin')
// Controllers within the `App\Http\Controllers\Admin` namespace
You need to remove namespace or create AdminController, UsersController, and BlogController under Controllers\Admin folder and namespace.

After adding a new directory into controllers, fatal error thrown

I am new to Laravel and after I added a new directory to the controllers viz, Admin, I updated the namespace also updated routes but somehow, I am getting a fatal error exception. Please help me figure out the problem
App->Http->Controllers->Admin
<?php
namespace App\Http\Controllers\Admin;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
routes.php
Route::get('/admin', 'Admin\AdminController#index');
snapshot of directory structure
try
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Note the "use" statement. I suspect that Laravel is looking for the Controller class, which this controller extends, but is unable to find it because of that missing statement.

Routing in Laravel is not working

I am using laravel 5.0
I am trying to route the following. But it is not working
Route::post('accesscontrols/permissions', 'AccescontrolsController#permission');
I don't know what error in this.
It does not access permissions function in AccesscontrolsController
I have a function in AccesscontrolsController
public function permission()
{
$roles = DB::table('roles')->get();
$permissions = DB::table('permissions')->get();
return view('accesscontrols.permission', compact('roles', 'permissions'));
}
What I have did wrong?
Your route declaration should be made in app/Http/routes.php.
Also, make sure that your controller is within the App\Http\Controllers namespace and that it extends App\Http\Controllers\Controller.
Ex:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function permission()
{
...
Also, if you whish to test it in the browser (by typing "accesscontrols/permissions" in the address bar), you route should answer to the GET verb. Try to declare it using Route::get( instead.
You are returning a view in your method and you are not working with any POST data, which is strange. Are you sure you want POST request and not GET?

Resources