I have a class that I want symfony2 to autoload so I can reference it from inside one of my registered services (I do not want to use the service container for this class). I dropped it into src/{Vendor}/{BundleName}/Services but I'm getting a class not found exception.
Do I have to explicitly request that that directory gets autoloaded in autoload.php?
There's got to be a better way.
I don't understand why you won't put this class in your bundle?
src/{BundleNamespace}/MyClass.php
or
src/{BundleNamespace/MyClass/MyClass.php
If you want your class to be bundle independant, then put it in it's own bundle library:
src/MyLibrary/MyClass.php
You can now use
MyLibrary\MyClass()
The src directory is the fallback in the autoloader, so you wont need to explicitly declare its namespace there, however you will need to comply with PSR-0
The namespace for my custom class was not correct. If you're putting a file in src/{VENDOR}/{BundleName}/Services, you must use namespace {VENDOR}{BundleName}\Services
Related
I can't make interfaces work in my php/laravel project. I have a series of Controllers, which I want to implement an interface. And PhpStorm seems happy with my syntax. But I get a runtime error that the interface is not found, when I make a check if the current object indeed has the interface:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use iTeamEntryController;
class TeamController extends Controller implements iTeamEntryController {...}
and then the Interface:
<?php
use Illuminate\Http\Request;
interface iTeamEntryController
{
public function saveTeam(Request $request);
}
Here I get an error on the line defining the class, saying that interface 'iTeamEntryController' is not found. Both the class and the interface exists in the same folder.
I've been trying to find an example online, but everyone either has both the interface and the class declaration in the same file, or uses 'include_once' or 'require_once', which to me seems to be referring to files, and not OOP. So what am I doing wrong here? Why can't my interface be found?
Remember that the use clause wants you to specify an absolute class/interface/trait name, not relative to your current namespace.
The below is wrong, unless your controller is in a global namespace (which I'm sure isn't the case):
use iTeamEntryController; // wrong
And this - below - is correct:
use App\Http\Controllers\iTeamEntryController;
If you keep your interface in app/Http/Controllers directory, don't forget to specify a namespace:
namespace App\Http\Controllers;
If you want, on the other hand, your interface to be in a root directory, make sure it is there. Then, the namespace is not needed and your use clause is correct. Except, I'm pretty sure you don't want your interfaces in the root directory of your Laravel app.
In my new laravel app I've added two custom classes. One loads fine when I use it in the controller, but the other, which is in another folder, does not work.
The working class, which I will call Working is located in app\Classes, it has the namespace namespace App\Classes and in the controller I call it with use App\Classes\Working.
The non-working class, which I will call NonWorking is located in app\Classes\NonWorking. I've tried giving it the namespaces namespace App\Classes and namespace App\Classes\NonWorking. From the controller I've tried calling it with use App\Classes\NonWorking and use App\Classes\NonWorking\NonWorking, but I get the error Class 'App\Classes\NonWorking' not found or Class 'App\Classes\NonWorking\NonWorking' not found.
I've been able to get it to run correctly by moving the NonWorking class into the same folder as the Working class and setting the namespace as namespace App\Classes, but the NonWorking class is from another repo and should be in its own folder as it will not be the only one from another repo.
So, how do I get Laravel to understand where this class is?
Laravel uses the PSR-4 autoloading. What it means is basically your class should follow the folder structure.
So if you have classes in app/Classes, they should have the namespace App\Classes.
So the file app/Classes/Working.php will have at its top namespace App\Classes; and to import it in another file, you write in the other file use App\Classes\Working;
If you have a class inside app/Classes/SubFolder, it should have the namespace namespace App\Classes\SubFolder;
So here is a class AmazingClass in app/Classes/SubFolder/AmazingClass.php file:
// app/Classes/SubFolder/AmazingClass.php
namespace App\Classes\SubFolder;
class AmazingClass
{
//
}
Let's use AmazingClass in another class.
// Some file in another namespace
namespace App\My\Random;
use App\Classes\SubFolder\AmazingClass;
// Rest of the file
Plus: Whenever you add a new class and you can't use it, it's likely that it's not autoloaded. Run the command
composer dump-autoload
to re-autoload the classes.
to solve your issue just create your folders and classes in App folder and run command :
composer dump-autoload
and they load all classes you have created
Unable to process queries in graphql as included classes is reported as not found
I am using Laravel version 5.8.8 with graphql plugin provided by https://github.com/rebing/graphql-laravel
'GraphQL' => Rebing\GraphQL\Support\Facades\GraphQL::class
Rebing\GraphQL\GraphQLServiceProvider::class,
Graphiql\GraphiqlServiceProvider::class,
You are including the Facade in your config.php, but you're not yet referring to the facade when you try to access it within one of the files in app/GraphQL/Query.
After defining your namespace in the file you want to use the Facade from, you will have to add a use statement for your facade:
<?php
namespace App\GraphQL\Query;
use GraphQL;
// ...
If you don't, PHP tries to auto-resolve the namespace of the class call to whatever namespace you are in currently (hence the error about not being able to find App\GraphQL\Query\GraphQL).
I am trying to integrate an existing library into Laravel5 which itself is not namespaced and uses its own classes in subfolders using require.
I have placed it at 'app/API/libname/mainlibclass.php'.
with sibling directory at 'app/API/libname/toolkit' which contains the classes the library uses.
Calling from a Laravel controller I am unable to create the class using a require statement (correct?) before
$objectinstance=new Mainlibclass();
so in the main Laravel app I have
use app/API/libname/Mainlibclass
then later the usual
$objectinstance=new Mainlibclass();
In the existing library and each of its own used classes I set
namespace app/API/libname
and 'use' where needed.
I now have no class not found but one of the files uses 'implements Iterator' - I am getting error Interface 'App\API\libname\Iterator' not found.
Try adding \ in front of that so it looks like this:
class ABC implements \Iterator {
Edit:
I think it would be better practice to keep external non-psr-4/0 libraries untouched (for easier update if needed in future) and outside of app/ directory.
You could use composer classmap autoload feature for this.
a quick (maybe stupid) question.
I'm using a namespace for my controllers, like so:
namespace Members;
use DB;
use Input;
use PerformanceReport;
use Redirect;
class AdminController extends MembersController {
And as expected, I have to provide use statements for the Laravel classes I wish to use.
From what I've understood, the composer autoloader prevents this from being necessary if used correctly.
So my question being, is it possible to configure the autoloader to suit my needs, and if so, how would I go by doing this?
Your question is connected with the way PHP namespaces work, not with composer's autoloader.
If your class is in namespace Controllers; and you'd write Redirect::to('/') php would assume that the class you're referring to is in the current declared namespace (in that case Controllers/Redirect). You can either write \Redirect::to('/') or put a use Redirect statement on top like you did.
Composer's autoload just maps namespaces to their file directory (see vendor/composer/autoload_classmap.php for how it maps it).
If you want to dive more into composer's autoloading, i'd recommend read up on PSR-0 and PSR-4.