Line in Linus
Line in Linus

Reputation: 420

Custom helper: class not found. Laravel 5.1

I've created a custom new file, app/Http/Helpers.php and added:

<?php

namespace app\Http;

class ConnectionsHelper {

    public static function organisation($id) {
       return 'ID:'.$id;
   }
}

In Composer.json, in the autoload-block I've added:

"files": [ 
    "app/Http/Helpers.php" 
]

And then I ran "composer dump-autoload".

My controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class myController extends Controller
{
    public function index()
    {
        echo ConnectionsHelper::organisation(2);
    }
}

And get in return:

FatalErrorException in OrganisationsController.php:
Class 'App\Http\Controllers\ConnectionsHelper' not found

Upvotes: 1

Views: 2603

Answers (1)

Andy Noelker
Andy Noelker

Reputation: 11269

You need to provide a namespace alias in your controller.

use App\Http\ConnectionsHelper

Autoloading a file does not mean that the classes in that file are required/included in all other scripts in the app. It just means that you are making those files available to your app. In this case, your helper file is already inside the App namespace which is autoloaded by default, so you can remove the files bit of your composer.json completely.

Upvotes: 4

Related Questions