I've just set up a new Laravel 5 project. I included a Helpers.php in the /app directory. Here it is:
<?php namespace EP\Helpers;
class Helpers {
public static function sayHi()
{
return 'Hi';
}
}
And in the route I am doing:
Route::get('/', function(){
return EP\Helpers\Helpers::sayHi();
});
But when I hit that route I am getting the error:
Class 'EP\Helpers\Helpers' not found
The funny thing is, PHPStorm is able to auto detect the namespace. Anyone know why this would be happening?
I believe in your case the namespace would be:
<?php namespace EP;
Related
I am a noob in laravel but never faced this issue in my previous 1-2 projects. I don't know why my custom route is not working, while the welcome route working fine. This is first-time I am getting this error 404 page not found.
web.php
Route::any('/dashboard', 'Admin\AdminController#tempirelounge')->name('dashboard');
Controller
namespace App\Http\Controllers\Admin;
//use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function tempirelounge()
{
return view('admin/index');
}
}
And the weird thing is that when I am trying to do this,
Route::get('/dashboard', function () {
return view('welcome');
});
It is not working
but when I do this
Route::get('/', function () {
return view('welcome');
});
It is working fine.
Does anyone have any idea why this is not working?
your Controller namespace is App\Http\Controllers, and in route u use ... Controllers\Admin\AdminController#tempirelounge where is true?
If your controller is in Admin folder then use correct name space.
Replace
App\Http\Controllers
with
App\Http\Controllers\Admin
Namespace is wrong
Approach 1:
Route::any('/dashboard', 'App\Http\Controllers\AdminController#tempirelounge')->name('dashboard');
Approach 2:
In web.php
use App\Http\Controllers\AdminController
Route::any('/dashboard', 'AdminController#tempirelounge')->name('dashboard');
Try to clear the route cache php artisan route:clear
I am familiar with Laravel 4 routes, but I am experiencing some problem with Laravel 5.
I code route.php as:
Route::get('/','HomeController#index');
and my HomeController.php is the following:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('index');
}
}
The output page displays as:
Whoops,looks like something went wrong.
The Route annotation file seems to be perfect.
The same case occurs for folder routing too!!
Please help me out.
First of all you should follow the instructions from James Njuguna in a comment to your question. Withoug debugging we can only guess whats going wrong.
In your case, most likely your error is, that the line
return View::make('index');
is causing an exception, because class App\Http\Controllers\View is not found. In this file a namespace is used, so you have to reference the root namespace like:
return \View::make('index');
OR you use a helper function
return view('index');
This function is documentated at http://laravel.com/docs/5.0/helpers#miscellaneous
If that's still failing... maybe you don't have an index.php or index.blade.php in your resources/views folder.
All, what #shock_gone_wild and #JamesNjuguna said is true. The reason of an error occurring is that you do not use namespaces when you call View.
For testing you can simply return text from a controller like this:
public function index()
{
return 'test'
}
and when it returns a result you can see what was the reason for the error and than you can change it with view global function, like #JamesNjuguna said.
Try this
public function index()
{
return view('home');
}
In laravel 5 the view class is not illuminated using a capital letter at the beginning
I was reading some tutorials on creating custom classes for Laravel. I followed instructions and did exactly what tutorials say:
Created new folder laravel/app/libraries/graphics/
Edited laravel/app/start/global.php where I added:
app_path().'/libraries/graphics',
Created new file in laravel/app/libraries/graphics/ named Image.php with this code:
<?php namespace graphics/Image;
class Image {
public static function hello() {
return 'Hello';
}
}
Used composer dump-autload command
Route::get('/' , function() { return Graphics\Image::hello(); } ); is returning error:
Use of undefined constant graphics - assumed 'graphics'
I also added "app/libraries/graphics/Image.php"line into composer.json autload section, which should not be neccessary. Why I am getting this error? Every tutorial shows the same procedure for this, but why it doesn't work?
Shouldn't your namespace just be graphics? The current file creates graphics\Image\Image. Try removing Image from your namespace.
<?php namespace graphics;
class Image {
public static function hello() {
return 'Hello';
}
}
Have you tried using artisan dump-autoload instead?
It will clear all of Laravel's compiled code.
See here: What are differences between "php artisan dump-autoload" and "composer dump-autoload"
You don't need to confusion for yourself. I'm resolve issue into Laravel 5. You don't need to add "app/libraries/graphics/Image.php"line into composer.json autload section because By default, app directory is namespaced under App and is autoloaded by Composer using the PSR-4 autoloading standard.
<?php
namespace App\libraries\graphics;
class Image {
public static function hello() {
return 'Hello';
}
}
and now use your Image Class from your route.
Route::get('graphics',function(){
echo \App\libraries\graphics\Image::hello();
});
The error I'm getting is that the controller doesn't exist even though I know it does, here's the code.
Route.php
Route::get('mdpay/template', array("uses" => "templateController#index"));
templateController.blade.php
class templateController extends BaseController {
public function index()
{
echo "made it";
}
}
Why might I be getting this error: Class TemplateController does not exist
================= UPDATE: ==================
Ok, so I've created the correct route, renamed my file, and corrected the class name and I'm still coming up with that error.
File Names:
templateController.php
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// app/views/myView.blade.php
echo "hello";
}
}
My route is:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Still receiving Controller Doesn't Exist error. All my other controllers (3 others) are working except this one.
If you are using the standard composer classmap autoloader you need to composer dumpautoload everytime you create a new file.
So to create a new controller with the standard composer setup given by Laravel:
Create a new file in app/controllers named TemplateController.php
Open up terminal and run composer dumpautoload
As previous users have said, only view files should end with .blade.php.
If you're using Laravel 8, add this line to your RouteServiceProvider.php (you can search it by using CTRL + P):
protected $namespace = 'App\Http\Controllers';
This solved the issue for me.
It should be:
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// return "made it"; // or
// app/views/myView.blade.php
return View::make('myView');
}
}
Route for that:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Use blade in a Blade view, i.e: myView.blade.php basically stored in app/views/ folder. Read more about blate template on Laravel website.
Controllers live in the app/controllers directory and should remain there unless you have your own namespaced structure.
The reason you're getting a Class TemplateController does not exist is because it doesn't, firstly, your class is called templateController and secondly, it exists as templateController.blade.php which wouldn't be loaded in this way.
Blade files are for views, and only views within app/views or a custom views directory should end with .blade.php.
Create the file app/controllers/TemplateController.php and add the following code to it.
class TemplateController extends BaseController {
public function index()
{
echo "made it";
}
}
Now on the command line, run the command composer dumpautoload and change you route declaration to:
Route::get('mdpay/template', array('uses' => 'TemplateController#index"));
Now it should all work.
In case you're using Laravel 9 and the error is like Illuminate\Contracts\Container\BindingResolutionException and Target class <controller name> does not exist. when trying php artisan route:list on terminal.
This is the setup that I do:
Add protected $namespace = 'App\\Http\\Controllers'; to RouteServiceProvider.php
Add 'namespace App\Http\Controllers;' to the controller file.
Do php artisan optimize on terminal
(Just to make sure the route already there) Do php artisan route:list again on terminal, and the controller route should be displayed.
I am putting my Controller called "LoginController" in a folder "login".
class LoginController extends BaseController{
public $restful = true;
//log in function
public function Login(){
// load the login page
return View::make('login.login');
}
}
In the routes, I give this:
Route::get('/',array('uses'=>'login.LoginController#Login'));
Also tried
Route::get('/',array('uses'=>'login\LoginController#Login'));
Route::get('/',array('uses'=>'login\Login#login'));
None of the above seem to work, and give me Class does not exist error.
I am very dumbstruck with this error. Is the way I am accessing the controller in the "uses" correct? Do I need to do any additional things before I can get it to work?
Any help really appreciated!
All you should need is
Route::get('/',array('uses'=>'LoginController#Login'));
Composer need to register this change in routes so dump-autoload composer
php composer.phar dump-autoload
Also if you are using laravel 4, then declaring restful controllers with
public $restful = true;
no longer works.
this happens to me often, just to give a different answer that worked for me
php artisan dump-autoload
Enjoy!
Yeah i had the same issue, i got my answer from https://stackoverflow.com/a/31638718/2821049
Route::group(['namespace' => 'login'], function(
{
// Controllers Within The "App\Http\Controllers\login" Namespace
Route::get('/','LoginController#login');
});
In class you adds :
namespace App\Http\Controllers\folder;
use App\User;
use App\Http\Controllers\Controller;
and in routes you call:
Route::get("admin/login","folder\class#NameFunctionInClass");
Note: folder is the name folder class contains