How can I access global classes from within a package? - php

I created a package which also contains his own controller files and also a router.php file.
My problem is that i can't access anyone of the main classes like Schema or View.
I always get a Error: Class not Found error.

I'm assuming you're using namespaces in your package, which you should be. As a result, you need to precede global classes with a backslash to access them. For example, View::make() becomes \View::make().
Alternatively, you could import the Facades:
<?php
namespace Your\Namespace;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
//...
View::make($view, $data);
See the PHP namespace FAQ.

Related

How can I correctly use/import an interface in a php class?

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.

How to use a custom class in Laravel when it is in a sub-folder?

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

Importing the same PHP class within multiple files of the same namespace

I would like to Use the same class Helper, located at App\Helpers\Helper, in two different files within the same namespace.
For example:
Class A:
<?php
namespace App\Services;
Use Helper;
class A {...}
Class B:
<?php
namespace App\Services;
Use Helper;
class B {...}
However, this is not allowed. The error is:
Cannot use App\Helpers\Helper as Helper because the name is already in use
I could rename the Helper class in my second file and say Use Helper as SomethingElse, but this seems like a messy solution especially if I want to use this Helper in many more than two classes.
Is there a workaround for this?
You most probably already have an included class that goes by that name, the likely problem is that you have a class by that name in another namespace (and said class has been included). You can use both classes (from different namespaces with the same name) by aliasing or using fully qualified names of the classes.
Read this properly.

phpcs - class must be in a namespace of at least one level - how to fix?

I am using laravel 4.2.
Lets say there is such class:
class BShopsController extends ShopsController
To fix this, I try to use name space lets say this:
namespace app\controllers;
and then it does not find ShopsController
so I add
use \ShopsController;
Then I get error:
Class BShopsController does not exist
What namespace should I use first of all so it would not break anything?
Edit:
BShopsController and ShopsController are in folder Shops
As your files are inside the Shops folder and I believe that the Shops folder is inside the app folder you should namespace your class the following way.
<?php namespace Shops;
class BShopsController extends ShopsController{}
Similarly,
<?php namespace Shops;
class ShopsController{}
So with the help of Shhetri and this Using namespaces in Laravel 4
I did this way:
namespace App\Controllers\Shops;
class BShopsController extends ShopsController{}
Also in routes.php then need to change to this:
Route::controller('shops', 'App\Controllers\Shops\ShopsController');
And where calling action() method - also need to use namespace.
Also needed to run
composer dump-autoload -o
otherwise were errors.
Also in ShopsContrller needed to to this:
use \App\Controllers\BaseController;
Because Shops controller was in another namespace than BaseController and cannot find it. But is extending from BaseController, so need it.

how to use different namespaces in a controller in laravel 4.1

What I want to do is to use different namespaces in a controller. I have a tree scheme like this:
app/
app/controllers/
app/modules/
app/modules/modulename/
app/modules/modulename/controllers/
app/modules/modulename/controllers/modulecontroller.php
app/modules/modulename/models/
app/modules/modulename/models/modulemodel.php
What I want to do is to call a model from a controller in app/controllers/ folder. Therefore I am supposed to add namespace as follows:
use App\Modules\Facebook\Controllers\Facebook;
The problem is that when I add a namespace and use App::() function at the sametime, I get the following error:
Class 'App\Modules\Modulename\Controllers\App' not found
I think it is looking the App::() function in module folder. How can I solve this problem?
if you use App inside your App\Modules\Facebook\Controllers namespace, it will be interpreted as App\Modules\Facebook\Controllers\Facebook\App class.
since you don't want to have the previous namespace, you use a \ before App like:
\App::()
or put a use statement of top the class like use App;
You probably are creating an unusual namspace scheme. It appears you are namespacing every class from your module differently. You should namespace your code within your module only, like so:
// Adding Onur to the namespace prevents any future namespace collisions.
<?php namespace Onur\Facebook;
After creating your namespace you should add all classes that are outside of your namespace that you want to use as followed.
use Eloquent, Input, Validate, Etc;
This prevents you from adding a \ in front of every class instance, making your code hard maintain and prone to errors. It also gives you a good overview on all the classes you are using in the current class.
if you say
use App\Modules\Facebook\Controllers\Facebook;
then you are supposed to use Facebook instead of App... Or donĀ“t I understand your problem correctly?
if you say
use App\Modules\Facebook\Controllers\Facebook as FacebookController;
the you can use FacebookController in your file
if you need Access to the root App, you need to to root it using a leading \
\App::make()

Categories