Namespace Laravel Installation - php

I just installed Laravel under xampp in a project with name blog1. The documentation says that default namespace is App.
I create the following controller :
namespace App\Http\Controllers;
use App\User;
use App\Repositories\UserRepository;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* The user repository implementation.
*
* #var UserRepository
*/
protected $users;
/**
* Create a new controller instance.
*
* #param UserRepository $users
* #return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
public function show($id)
{
$user = $this->users->find($id);
return view('user.profile', ['user' => $user]);
}
}
with the name testController.php under public directory.
when I http://localhost/blog1/testController.php
I get the following Class 'App\Http\Controllers\Controller' not found in C:\xampp\htdocs\blog1\public\testController.php on line 10.
Any suggestion?
I tried to set the namespace to blog1 but now nothing happened with blog1/Http?Controller.
I reinstall Laravel through composer.

Read about PSR-4, PSR-0 (deprecated) and autoloading in PHP. Error is thrown because class name (UserController) must be exact to the filename testController.php so you should name your file UserController.php or your class testController.
Also read about Laravel's routing and composer's autoloading.

The error from laravel is valid.
You're placing your file TestController.php under public directory, and giving the class a namespace of - App\Http\Controllers.
You should place your TestController.php inside the directory:
app > Http > Controllers
By this, your code will not throw an error again!
See more about Laravel Controllers & Namespacing & Laravel Directory Structure
Hope this helps!

Related

Class does not exist exception if I don't provide full path to my class in Laravel validation extension

I am using Laravel 5.8 and attempting to set up a custom validation extension.
I have created a class GroupValidator containing a validate function.
I have created a ValidationServiceProvider with the following code:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
use App\Classes\GroupValidator;
class ValidationExtensionServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
Validator::extend('valid_parent_id', 'GroupValidator#validate');
}
}
When my validation triggers I get a Class GroupValidator does not exist exception. However if I specify the full path to my class in the extend function call like so:
Validator::extend('valid_parent_id', 'App\Classes\GroupValidator#validate');
then everything works fine.
Is there some way I can set this up so that I don't have to include the full path to my class?

Laravel 5.7 Class App\Http\Controllers\Auth\SendsPasswordResetEmails does not exist

I'm trying to implement the reset password function using the built-in function from Laravel 5.7 as i have defined my routes in my web.php. I tried running php artisan route:list , It gave me an exception
UPDATE
Sorry for the lack of information given. I have already ran the command php artisan make:auth previously and the Auth::routes() has already been defined in web.php I am trying to access function resets in ResetPasswords traits through my ResetPasswordControllerbut it gave an exception
Class App\Http\Controllers\ResetPasswordController does not exist
I am using the pre-defined controller that is located at App\Http\Controllers\Auth\ResetPasswor.php
ResetPasswordController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
public function reset(Request $request){
$reset = $this->reset($request);
}
/**
* Where to redirect users after resetting their password.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
web.php
Auth::routes();
Route::post('password/reset','ResetPasswordController#reset');
SOLUTION
I have figured out where did i do wrong i had to add a Auth\ in my routes
Route::post('password/reset','Auth\ResetPasswordController#reset');

Laravel: ReflectionException - Class App\Http\Controllers\XXXX does not exist

I am trying to run test page using Laravel.
When I use Controller, every time I got message:
Laravel: ReflectionException - Class App\Http\Controllers\XXXX does not exist
Does somebody knows where problem is?
This is my routes/web.php:
Route::get('/hi', 'HiController#index');
HiController.php (it is in correct folder structure: app/Http/Controllers/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HiController extends Controller
{
public function index(){
return "test";
}
}
RouteServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* #return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* #return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* #return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
Also, when I use this code in routes/web.php it works:
Route::get('/hi', function (){
return "hi";
});
It's clean Laravel 5.6 installation, on Windows, wamp64.
I tried also with "composer dump auto-load" and "php artisan config:clear" but nothing works.
Thank you in advance.
Run the following (assuming your app name is app):
php artisan app:name app
Then use the following namespace also in your controller:
app\Http\Controllers\Controller
Q: why XXXX if it's HiController? About the issue, if it returns the hi, technically, it has to work if it's vanilla.
Since its on windows and wamp64, I don't think file permissions are a thing so let's skip that.
Make sure the name of the file is indeed correct
Make sure the file indeed exists in the correct directory
Make sure the namespce is namespace App\Http\Controllers;
Add this use, just in case: use App\Http\Controllers\Controller;
Just to be sure, make sure in your composer.json you've got this (you should have since it's default):
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
If none work, try creating a new controller php artisan make:controller SomeController and try it with this one.
tried every option, the only that worked for me was restarting the vm, ngingx or laravel had something cached or something similar that throw this error.

Defining controller in namespace still Class App\Http\Controllers error taking place in laravel 5.5

I am working in laravel 5.5. I have created a controller folder with name Abc and inside it created a controller file with name abc.php, inside this file I have written code:
namespace App\Http\Controllers\Abc;
use App\Http\Controllers\Controller;
//use Illuminate\Support\Facades\DB;
class abcController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function NewPromotion()
{
return view('new-promotion');
}
}
And in Route folder under web.php file I am calling its view file, like:-
Route::get('/new-promotion','Abc\abcController#NewPromotion');
In spite of defining in namespace I am still getting error i.e.
"ReflectionException (-1)
Class App\Http\Controllers\Abc\abcController does not exist".
What could be the possible issue?
You should make sure your file is located in app/Http/Controllers/Abc directory and has name abcController.php (case sensitive).
Also by convention class names start with big letter, so you should rather name your controller AbcController and have this file with name AbcController.php (and also update your route file to use valid case of this controller name).
Good way to create controller is use cmd (you have to be in project directory)
php artisan make:controller "Abc\abcController"

Laravel 5.3, Class cant be found, While the route is correct

I am currently working on view composer:
The problem right now that i have, The route that im calling inside the ComposerServiceProvider.php says that it cant find the route to the ViewComposers/LespakketComposer.php
In the Config\app.php i did add the correct App\Providers\ComposerServiceProvider::class,
Here is my code in ComposerServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
class ComposerServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
View::composer('*','App\Http\ViewComposer\LespakketComposer');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Here is my error:
ReflectionException in Container.php line 734:
Class App\Http\ViewComposer\LespakketComposer does not exist
The Routes in my folder structure
Does anyone has a solution for my problem?
( The file i am requesting is an Class Indeed )
You forgot a s:
App\Http\ViewComposers\LespakketComposer

Categories