I am in trouble with namespaces in lumen/laravel. My project structure is following:
App/Http/Controllers
ExampleController
If I would call my Controller by a route:
Route::get('/', 'ExampleController#test');
I've got the exception, that this class App\Http\Controllers\ExampleController is not found! In my ExampleController I've signed it with namespace App\Http\Controllers;
Is there any explaination, because I dont changed the default namespace and I need to group it with namespace => App\Http\Controllers. If I use that namespace grouping, it would work?!
Related
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
I am new to Lumen, and I got error when using Postman: ReflectionException Class App\Http\Controllers\PostsController does not exist
Here is my PostsController.php
namespace App\Http\Controller;
use App\Post;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostsController extends Controller{
/*content*/
}
I already checked location of my controller. I also read the same problem and tried the solutions, but it was not work for me. Can anyone tell me how to fix this? Thanks before
Controller's namespace is wrong, Check the namespace. Good luck
namespace App\Http\Controllers;
You have wrong namespace in your controller file.
You need to change it to Controllers
namespace App\Http\Controllers;
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
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;
There is a HomeController in app/Http/Controllers and a Player model in app.
HomeController:
namespace App\Http\Controllers;
Player:
namespace App;
use Illuminate\Database\Eloquent\Model;
Double-checked, the Player model gets PSR-4 autoloaded. I've tried to use various versions in HomeController, but it keeps trying to look for Eloquent class (which Player extends) in wrong paths. Also, in the tested HomeController method, instantiating the model is foggy, do I need to prefix the namespace now? because with or without use command it does not work.
Realized that I had to alias this path to Eloquent to make it work:
use Illuminate\Database\Eloquent\Model as Eloquent;