Fresh laravel lumen 5.8 installed but controller name cannot be found in route
I created UserController in App\Http\Controllers\User
User Controller Content:
namespace App\Http\Controllers;
class UserController extends Controller {
public function index() {
return 'User list';
}
}
Loaded in route:
$router->get('/user', 'User\UserController#index')
Error:
Class App\Http\Controllers\User\UserController does not exist
I also tried with absolute namespace App\Http\Controllers\User\UserController#index but it still doesn't work.
It works if I don't put UserController in User folder
Change your UserController's namespace to:
namespace App\Http\Controllers\User;
class UserController extends Controller
{
// ...
}
Try changing namespace of the controller to
namespace App\Http\Controllers\User;
class UserController extends Controller
{
// ...
}
Related
I tested to using laravel but i get some error unexpected, i already follows tutorial and some video. i think, i do it right. but i get this error. The problem controller not found media model.
Class "App\Media" not found
Route
Route::get('/', [MediaController::class, 'index']);
MediaController
namespace App\Http\Controllers;
use App\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Media model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Media extends Model
{
protected $table = 'media';
public $timestamps = false;
protected $primaryKey = 'Media_ID';
}
Your use statement for your Media model is incorrect. As of Laravel 8 the default namespace for models is App\Models where previously they where in the App namespace.
namespace App\Http\Controllers;
use App\Models\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Ensure that the Media.php file exists in your app/Models directory and is capitalised correctly.
Then in your web.php file, don't forget to include the use statement for your MediaController at the top.
use App\Http\Controllers\MediaController;
Use this code:
Your Controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MediaController extends Controller {
public function index() {
return Media::all;
}
}
Your Route:
Route::get('/', 'MediaController#index');
I have controllers in subfolders
--Controllers
----Admin
--------UserController
--------AccountController
----User
--------UserController
--------AccountController
I write routes for it
$routes->group('user', function ($routes){
$routes->get('dashboard', 'UserDashboard::index');
$routes->get('changePassword', 'User\AccountController::changePassword');
});
It give me 404 even though I have method and have something output as well
Controller code
<?php namespace App\Controllers;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
public function index()
{
echo "Hello";
exit();
return view('user/account/changePassword');
}
public function changePassword()
{
echo "Change Password View";
exit();
return view('user/account/changePassword');
}
}
You need to change the namespace of your controllers.
User controllers
<?php namespace App\Controllers\User;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
}
Admin controllers
<?php namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
}
I am using laravel 5.6, when create controller and when run controller through route, I am facing error like
Declaration of App\Http\Controllers\XyzController::xyz(Illuminate\Http\Request $request) should be compatible with App\Http\Controllers\Controller::xyz($job)
My Code is
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request)
{
return view('xyz.xyz');
}
}
Missing route parameter: $job
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request, $job)
{
return view('xyz.xyz');
}
}
The base Controller that XyzController extends from defines a method named xyz with a different signature than the one you are defining.
You will need to adjust the method in XyzController to match the signature of xyz in the base Controller or adjust the base Controller to have a different signature.
Example of the problem:
class A
{
public function xyz($obj) {}
}
class B extends A
{
public function xyz(Illuminate\Http\Request $request) {}
}
Declaration of B::xyz(Illuminate/Http/Request $request) should be compatible with A::xyz($obj)
You forgot to use controller?
use App\Http\Controllers\Controller as Controller
Using php artisan make:request StoreUserData I've create my rules to the request:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserData extends FormRequest {
public function rules(){
return [
'name'=>'required|integer',
'surname'=>'required|max:255|string',
];
}
}
And I'm trying to use it in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDataController extends Controller {
public function store(StoreUserData $request){
return 'valid';
}
}
Here the error I get: Class App\Http\Controllers\StoreUserData does not exist.
PS. Is not a problem of routing.
I'm following this documentation since I'm using Laravel 5.6 https://laravel.com/docs/5.6/validation#creating-form-requests
Actually when you use StoreUserData in controller method then you have to import that class otherwise it will assume the class is in App\Http\Controllers namespace and thats why it throw Class App\Http\Controllers\StoreUserData does not exist.
just add the below import at top of your controller class
use App\Http\Requests\StoreUserData
I am starting to learn about Laravel 5 repository pattern, and I can't get anything right! I probably used wrong namespace or 'use'; This is the error i am getting: Class App\Http\Controllers\ServiceController does not exist
This is my Controller:
namespace App\Http\Controllers;
namespace Repo;
use App\Http\Controllers\Controller;
use Repo\ServiceRepository;
class ServiceController extends Controller {
public function __construct(ServiceRepository $service){
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Here's the repository:
class ServiceRepository {
public function getAllServices(){
return \App\Service::all();
}
}
And here's my route:
Route::get('/services', 'ServiceController#showServices');
I've tried to change some namespaces but i end up with more errors :/
You have to read a bit about namespaces in PHP.
If your ServiceController is located in app/Http/Controllers directory, then the code has to be like follows:
<?php
namespace App\Http\Controllers;
use Repo\ServiceRepository;
class ServiceController extends Controller
{
public function __construct(ServiceRepository $service)
{
$this->service = $service;
}
public function showServices()
{
$services = $this->service->getAllServices();
return view('services', compact('services'));
}
}
Note that I removed namespace Repo and the first use statement.
A class has to have only a single namespace, so I removed the second Repo namespace.
Namespaces are really simple. In most cases namespace of a class just reflects a path to that class. In your case, it's App\Http\Controllers because your controller is probably located in app/Http/Controllers directory.
I've removed use App\Http\Controllers\Controller because your ServiceController and App\Http\Controllers\Controller are in the same directory, so they are in the same namespace. When two classes are in the same namespace, a full path with a namespace is not necessary for them to be able to access each other.
Hope that this helps.