Can't find new controller created - php

I everyone I've created a new Controler inside my App\Controllers\Admin folder and I already have two files called AdminInquiriesController and AdminUsersController.
When I run my app it says that
Class App\Http\Controllers\AdminNewsController does not exist
I don't undersantd. In all my 3 files inside this folder I'm using the namespace namespace
App\Http\Controllers
if it's working for the others why is not working for this?
<?php
namespace App\Http\Controllers;
use App\Manager\InquiryManager;
use Auth;
use Illuminate\Http\Request;
use function GuzzleHttp\json_decode;
use App\Model\InquiryStatus;
use Carbon\Carbon;
use App\Manager\UserManager;
class AdminInquiryController extends Controller {
<?php
namespace App\Http\Controllers ;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
use App\Manager\NewsManager;
class AdminNewsController extends Controller {
In my route file..
// inquiries
Route::get('/admin/inquiries', 'AdminInquiryController#search');
// news
Route::get('/admin/news', 'AdminNewsController#search');
Route::post('/admin/news/new', 'AdminNewsController#create');
I know this is so silly but I'm not understanding what's happening...

You should to define namespace in your controller file;
namespace App\Http\Controllers\Admin ;
Also change your web.php route file to
Route::get('/admin/news', 'Admin\AdminNewsController#search');
Or you can define namespace in routes group by
Route::namespace('Admin')->group(function () {
Route::get('/admin/news', 'AdminNewsController#search');
}
Also you have to put
use App\Http\Controllers\Controller;
In your controllers files in Admin folder

Try to add a second forward slash when writing model location
something like this
--model=App\\Models\\ModelName

Related

Target class [App\Http\Controllers\Admin\Category\CategoryController] does not exist

I am getting really confused on how namespace and use are being used here to reference my CategoryController. I am using laravel 8. Any guidance would be greatly appreciated!
My route uses resource and that seems to be ok.
web.php:
namespace App\Http\Controllers\Admin\Category;
Route::get('/admin/categories', [CategoryController::class, 'category'])->name('categories');
My Category Controller is App\Http\Controllers\Admin\Category\CategoryController.php as shown here is my folder structure:
CategoryController.php:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
public function __construct()
{
$this->middleware('auth:admin');
}
public function category(){
echo "Hello Category";
}
}
Your namespaces should mimic your directory structure. So if you have a CategoryController.php file that resides in the folder app/Http/Controllers/Admin/Category, then the namespace for that file would be namespace App\Http\Controllers\Admin\Category;
namespace App\Http\Controllers\Admin\Category;
use App\Http\Controllers\Controller;
class CategoryController extends Controller
{
// stuff
}
When referencing classes, you include the relevant namespace with the use statement.
So in your web.php file you want to replace:
namespace App\Http\Controllers\Admin\Category;
with:
use App\Http\Controllers\Admin\Category\CategoryController;

Class does not exist when up project to hosting

In localhost it runs ok, but when code is deployed to hosting there occurs an error in router/web.php. Code :
Route::get('about',"HomeController#index");
and in App\Http\Controllers\ I have file HomeController.php that contains:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;
use App\TheLoai;
use App\LoaiTin;
use App\TinTuc;
use App\User;
use App\Slide;
use DB;
class HomeConTroller extends Controller
{
//...
But it throws this error:
Class App\Http\Controllers\HomeController does not exist
How can I fix it?
HomeConTroller - should be HomeController - only C is capital
Change the controller's name from HomeConTroller to:
class HomeController extends Controller
{
//
}
Then run this to terminal:
composer dumpautoload

Getting Controller does not exist after moving my controller to sub folder in laravel 5

I have created a resource full route which was pointing to a CRUD controller in App/Http/Controller/SeatController.php
my Route in api.php
Route::resource('websites', 'SeatController');
I wanted to put all controllers related to Seat inside a directory. I created a directory named Seat and I moved all of my controllers related to Seat to it. it's path is : App\Http\Controllers\Seat\SeatController
before change it's path was : App\Http\Controllers\SeatController
After above changes I added Seat directory name to my resource full route like :
Route::resource('websites', 'Seat\SeatController');
But when I'm reaching this controller I get below error message :
Class App\Http\Controllers\Seat\SeatController does not exist
UPDATE :
my SeatController.php file is like :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Seat;
use Auth;
use Validator;
class SeatController extends Controller
{
When I change it to :
namespace App\Http\Controllers\Seat;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Seat;
use Auth;
use Validator;
class SeatController extends Controller
{
I Get this error message :
SeatController.php line 12:
Class 'App\Http\Controllers\Seat\Controller' not found
any one knows how to fix this ?
Class App\Http\Controllers\Seat\SeatController does not exist
This means controller can not find out the SeatController class. You should check following steps. Hope this will be worked for you.
First you have to check model call in your SeatController file is ok or not. And then check your namespace in SeatController is ok or not. Try with Something like below:
namespace App\Http\Controllers\Seat;
use App\Seat; \\type your Model name instead of Seat

How to use methods from one controller in another

I have installed waavi package for manipulation of translation files. I need to use methods from it's controller to mine? I tried something like this but it doesn't work
LanguageRepository::findByLocale(1);
This is what I am using in beginning of my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
use Waavi\Translation\Repositories\TranslationRepository;
use Illuminate\Foundation\Application;
If you have successfully done all the steps in here, you should be able to access to LanguageRepository using depedency injection(" It is recommended that you instantiate this class through Dependency Injection")
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
class DefaultController extends Controller
{
private $language_repository;
function __construct(LanguageRepository $language_repository)
{
$this->language_repository = $language_repository;
}
public function index()
{
dd($this->language_repository->findByLocale("en"));
}
}
Note: you need pass language string instead of id to findByLocale method. see line 97

Class Controller not found in laravel 5

So I am new to laravel and was just trying out some code to clear the basics,but however after creating a new controller to handle a route.It throws a fatal exception saying Class 'Controller' not found!
(The controller.php exists in the same directory)
The controller.php code is the default one
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
This is my PagesController.php code
<?php
class PagesController extends Controller
{
public function home()
{
$name = 'Yeah!';
return View::make('welcome')->with('name',$name);
}
}
This is route.php code
<?php
Route::get('/','PagesController#home');
The welcome.blade.php code is the default one except it displays the variable $name instead of laravel 5.
What am I getting wrong?
When you reference a class like extends Controller PHP searches for the class in your current namespace.
In this case that's a global namespace. However the Controller class obviously doesn't exists in global namespace but rather in App\Http\Controllers.
You need to specify the namespace at the top of PagesController.php:
namespace App\Http\Controllers;
You will want to specify the namespace to your Controller class:
class PagesController extends \App\Http\Controllers\Controller
otherwise Controller is looked up in the default root namespace \, where it does not exist.
My issue was a different class name than the one in the class that extends controller, the names should be same
If you are creating a controller inside a subfolder with a different namespace then use this on your controller file:
use App\Http\Controllers\Controller;

Categories