helper class not found in laravel 5 - php

I have create Helpers folder inside app, then I have created php file amchelpers.php ---> app/Helpers/amchelpers.php
amchelpers.php code:
<?php namespace App;
class AmcHelper {
static function displayString($string){
return $string;
}
}
then added these lines to composer.json
"files": [
"app/Helpers/amchelpers.php"
]
then run this command:
composer dump-autoload
then added 'Helper' => app_path() . '\Helpers\AmcHelper' to aliases array in config/app.php file.
in my controller I have below action (this action defined in route.php):
use Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}
when run the page http://localhost:8080/easy_marketing/public/displayMyString
I Got:
ErrorException in compiled.php line 6367: Class 'C:\wamp\www\easy_marketing\app\Helpers\AmcHelper' not found

you have written user Helper instead of use Helper
or
another way to achieve this is
Laravel 5 App directory is autoloaded by default with its folder, what you have to take care is add namespace followed by directory name,
so directory structure is App --> Helpers
so your name space must include App\Helpers
try following code
<?php namespace App\Helpers;
class AmcHelper {
static function displayString($string){
return $string;
}
}
and when you are using this class in another class write this after namespace declaration
use App\Helpers\AmcHelper as Helper;
class UserController extends Controller {
public function displayMyString(){
echo Helper::displayString('Hello');
}
}

Related

FatalThrowableError Class 'App\Models\Patient' not found in laravel 5.4

I have created a model by using the command php artisan make:model Patient. The command creates a model named Patient in the app folder.
In Model Patient:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Patient extends Model
{
//
public function getPurchaseOrder(){
return "Hello World";
}
}
In Controller PatientController:
namespace App\Http\Controllers;
class PatientController extends Controller
{
protected $patientModel;
public function __construct(Request $request, PatientInterface $patient)
{
parent::__construct($request);
$this->patientModel = new \App\Patient();
}
public function test(){
echo $this->patientModel->getPurchaseOrder();
}
}
It's working fine. The problem is when I create a folder named Models inside the app folder and move Patient model then call the model function it gives an error:
FatalThrowableError Class 'App\Models\Patient' not found
Any help will be appreciated.
When you move a class to a different directory, for it to be loaded by composer with the PSR-4 standard, you must also update the class' namespace to match.
namespace App\Models;
In addition, when you run the make command, you can include a namespace in that to automatically put it in the directory with the correct namespace:
php artisan make:model Models\\Patient

Getting "Class 'app\Http\Controllers\Controller' not found" in Laravel 5.1

I'm quite new to Laravel and when I am going through a tutorial when I encountered this error. This is my code in 'testController.php'.
<?php
namespace app\Http\Controllers;
use app\Http\Controllers\Controller;
class testController extends \app\Http\Controllers\Controller {
public function getAbout()
{
return view('Learning.about') ;
}
public function getHome()
{
return view('Learning.index');
}
}
And this is my 'routes.php'.
<?php
Route::get('test', [
'as' => 'test',
'uses' => 'testController#getHome',
]);
Route::get('about', [
'as' => 'about',
'uses' => 'testController#getAbout',
]);
I am getting this error:
Class 'app\Http\Controllers\Controller' not found
How can I fix this error?
Let's go through this step by step.
1. Check autoload directive on composer.json
Open composer.json file on your project root directory. Locate the the autoload section. It should be looking like this:
{
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
}
Make sure you have this configuration under the psr-4 option:
"App\\": "app/"
This configuration tells the composer that we want to autoload all classes inside the app directory using psr-4 convention and places it under the App namespace.
2. Update your controller
First, your controller file name should be in CamelCase style. So we have to renamed it to TestController.php. Make sure that it's saved under app/Http/Controllers directory.
Now open your TestController.php file, we have to capitalize the namespace and class name like so:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function getAbout()
{
return view('Learning.about') ;
}
public function getHome()
{
return view('Learning.index');
}
}
Note that we also turn this line:
class testController extends \app\Http\Controllers\Controller
Into:
class TestController extends Controller
Since we already import the base Controller class, we don't have to specify the fully qualified name. We imported the Controller class using the use keyword:
use App\Http\Controllers\Controller;
Save your TestController.php file.
3. Update your routes file
Now we have to update our app\Http\routes.php file. We just need to capitalize the controller name:
<?php
Route::get('test', ['uses' => 'TestController#getHome', 'as' => 'test']);
Route::get('about', ['uses' => 'TestController#getAbout', 'as' => 'about']);
4 Update your autoloader
Now the last thing to do. Open your terminal / command prompt. Go to your project directory and run the following command:
composer dump-autoload
This command will update the autoloader file (Read more here).
Now if you open up your browser and hit /test route, you should see the content from resources/views/Learning/index.blade.
Use correct namespace:
namespace App\Http\Controllers;
// Remove: use app\Http\Controllers\Controller;
class testController extends Controller {
According to my experience in Laravel projects, the namespaces starts with the capital A of App used in namespace, you should try to change your code to this:
namespace App\Http\Controllers;
class testController extends Controller { }
Also check if the controller - App\Http\Controllers\Controller lies in the same namespace as mentioned in your code.
Include this at the top of your Controller file. This fixed it for me.
namespace App\Http\Controllers;
In some cases the problem is that the framework is not able to instantiate your given controller class. This can happen for example if you are using a sub-folder under Controllers and that when you are extending the Controller.php class, you did not provide the use statement to that definition*. Other run-time errors may also cause this.
*Which is now required since your own controller is not at the root of the Controller folder anymore.

how to make a directory in controller - laravel

I created a Panel directory inside Controller directory .
there is a login function inside AdminController.php
class AdminController extends Controller
{
//
public function login()
{
return 'test';
}
}
in routes.php I wrote a route like this:
Route::get('/cp/login','Panel\AdminController#login');
but when I run below url I got some errors that there isn't exist this controller :
http://localhost:8000/cp/login
ReflectionException in Route.php line 280: Class
App\Http\Controllers\Panel\AdminController does not exist
Try adding the appropriate namespace to the top of the AdminController file, you will also need to specify the namespace for the Controller class that it extends, as they are under different sub-namespaces.
You can read more about PSR-4 autoloading here http://www.php-fig.org/psr/psr-4/.
Based on the directory structure that you have there it should read
<?php
namespace App\Http\Controllers\Panel
use App\Http\Controllers\Controller;
class AdminController extends Controller {
//..
}
you should add the namespace to the contorller
Change you namespace to
namespace App\Http\Controllers\Panel;
Laravel will resolve controllers based on your name spacing, not on your directory structure.

Laravel custom view helpers stop working if using namespace

I have followed some tutorials to create some global helper functions to use in blade views.
I have created ViewHelpers.php file in App\Helpers folder. This file contains the following code:
<?php
class ViewHelpers {
public static function bah()
{
echo 'blah';
}
}
Here is my service provider which loads my helpers (currently just one file):
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider {
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
echo $filename; // for debugging - yes, I see it is getting called
require_once($filename);
}
}
}
I have added it to config\app.php in 'providers' section:
'App\Providers\HelperServiceProvider',
And now I call my helper in a blade view:
{{ViewHelpers::bah()}}
For now it works fine.
But if I change my ViewHelper namespace to this:
<?php namespace App\Helpers;
class ViewHelpers {
// omitted for brevity
my views fail with Class 'ViewHelpers' not found.
How do I make my views to see the ViewHelpers class even if it is in a different namespace? Where do I add use App\Helpers?
Another related question - can I make an alias for ViewHelpers class to make it look like, let's say, VH:bah() in my views?
And I'd prefer to do it in simple way, if possible (without Facades and what not) because these are just static helpers without any need for class instance and IoC.
I'm using Laravel 5.
You will get Class 'ViewHelpers' not found because there is no ViewHelpers, there is App\Helpers\ViewHelpers and you need to specify namespace (even in view).
You can register alias in config/app.php which will allow you to use VH::something():
'aliases' => [
// in the end just add:
'VH' => 'App\Helpers\ViewHelpers'
],
If your namespace is correct you do not even have to use providers - class will be loaded by Laravel.

Laravel 5: Using a controller in a view composer - controller class not found

I followed these instructions and created a view composer for my default layout.
My DefaultComposer.php is located under app/Http/ViewComposers, hence the namespace used below:
<?php namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class DefaultComposer {
public function compose(View $view) {
$data['language'] = LanguageController::getDefaultLanguage();
$view->with($data);
}
}
?>
Now, when I load a page, I get the following error:
Class 'App\Http\ViewComposers\LanguageController' not found
This happens because the LanguageController.php is placed under app/Http/Controllers, which is a different namespace.
How can I use the LanguageController class in my DefaultComposer?
Update:
Using this declaration:
use App\Http\Controllers\LanguageController as LanguageController;
throws: Class 'App\Http\Controllers\LanguageController' not found. I'm confused.
I figured this out. As the controllers in my application live in the global namespace, all I needed to do is add a backslash in front of the class name.
So instead of this:
$data['language'] = LanguageController::getDefaultLanguage();
I did this:
$data['language'] = \LanguageController::getDefaultLanguage();

Categories