laravel 5 Class implements Iterator - php

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.

Related

Disambiguating a PHP namespace used by two vendors

I have two composer packages in my app that are creating a namespace collision in a third party module. I'm trying to determine the least disruptive way to disambiguate within the third party module so that I don't have to fork the packages.
First package from Vendor X, located in a directory structure like
path/to/vendor/VendorX/MailPackageA:
<?php
namespace MailPackageA;
class Mail {
...
}
My existing third-party module, which lists VendorX/MailPackageA as a dependency, located in structure like
moduleDirectory/MailingModule/mail.inc:
<?php
class ThirdPartyMailMod implements CoreMailSystem {
$mail_object = new MailPackageA\Mail();
. . .
}
For slightly different mailing functionality used in another module, I had to add a package that uses the same namespace identifier (MailPackageA), but from a different vendor, "VendorY". Like VendorX/MailPackageA, it's placed in a directory structure like
path/to/vendor/VendorY/MailPackageA:
<?php
namespace MailPackageA;
class Mail {
...
}
After adding this, I get errors in MailingModule/mail.inc at $mail_object = new MailPackageA\Mail(); because of the obvious namespace collision between MailPackageA\Mail classes from VendorY and VendorX.
Routes I've already been down, but failed:
Modify PSR-4 autoload rules in composer.json for either of the packages to create new namespaces. (I'm getting the sense my framework -- Drupal 7 -- does not respect the composer autoload rules, and simply defaults to stock php 7 namespacing), so I've abandoned that for the moment.
Try to add a vendor prefix alias to the MailingModule/mail.inc file with use.
<?php
use VendorX\MailPackageA;
\\ also tried the `use VendorX\MailPackageA as MailPackageA;` pattern to no effect
class ThirdPartyMailMod implements CoreMailSystem {
$mail_object = new MailPackageA\Mail();
}
I suspect that my app doesn't know about the vendor directories. Not sure how to force that.
What's the least disruptive way (i.e. minimal forking/patching) to make sure MailingModule only uses the MailPackageA\Mail class from Vendor X?
to overcome this problem use alias when you use the namespace .
use as keyword like this
use VendorX\MailPackageA as newPackageName;
and if you need to call the class from this namespace you will have it like this
$packageName = new newPackageName\Mail();

Putting Laravel and Lumen controllers inside packages

I'm developing a package which has controllers in it and I want this package to be compatible with (or useable by) both Laravel and Lumen projects. My problem is Laravel controllers extend Illuminate\Routing\Controller and Lumen controllers extend Laravel\Lumen\Routing\Controller. The controller inside my package can't extend them both.
The only solution I've come up with is to have the controllers inside the package extend App\Http\Controllers\Controller.
But I see some problems:
App\Http\Controllers\Controller should exist; which means it wouldn't work if the App namespace is named differently.
The package is now "aware" that it is being included in something.
Testability: I can't test the controller independently because of the reference to App\Http\Controllers\Controller.
Is there a better way of doing this?
Edit 1
I'm finding other classes which are affected in a similar way. For example, the namespace of the trait Authorizable is Illuminate\Foundation\Auth\Access in Laravel while it is Laravel\Lumen\Auth in Lumen. I am using a model which uses that trait. How do I make my model compatible with both Lumen and Laravel?
Well, you could simply have two different files and classes wrapped in if statements and check for the corresponding classes to extend. So:
LaravelClass.php:
if(class_exists(Illuminate\Routing\Controller:class)){
class LaravelClass extends Illuminate\Routing\Controller {
use YourCodeTrait;
// any more code that is not in your trait
}
}
LumenClass.php
if(class_exists(Laravel\Lumen\Routing\Controller:class)){
class LaravelClass extends Laravel\Lumen\Routing\Controller {
use YourCodeTrait;
// any more code that is not in your trait
}
}
Loading both files will only load one of the classes. In the code above I use a trait to load the contents of your controller, assuming the contents is the same, you could use the same trait and not have to repeat yourself.
trait YourCodeTrait{
// whatever would normally go in your controllers
}

Extending all controller classes from my custom controller class in symfony2

Like in Codeigniter we do have 'core' folder where we can define our own controller like 'MY_Controller' and can be used to extend all the class to extend from this controller is there any possibility to do so in Symfony2.
In symfony I want to create class 'MY_Controller' which extends from the base class 'Controller', and I want all the classes in the controllers to extend from MY_Controller' class.
Thanks in Advance...
Note:
When working with Symfony2 it is strongly recommended you follow the Symfony2 coding style. It's basically the same as PHP-FIG, with one or two deviations. So underscores are a no-no in class names. Other than that: Symfony is pretty easy to work with, and fully OO, so changing the class a controller extends from is as simple as replacing extends Controller with extends AnotherClass.
But now, the symfony2-way of using a custom controller:
What you could do, is create a Core bundle (CoreBundle henceforth). Then, in this CoreBundle, define a controller, that extends from the Symfony Controller component. From the command line, in your project root, use this command:
php app/console generate:bundle --namespace=YourNameSpace/CoreBundle --bundle-name=YourNameSpaceCoreBundle
More options can be found here
After that, you'll find a DefaultController class in the bundle directories. (probably in the folder src/YourNamespace/CoreBundle/Controller). Then, set about generating your Core controller:
php app/console generate:controller --controller=YourNameSpaceCoreBundle:Core
See the documentation for more options on how to generate your core controller.
After you've finished setting up your custom controller, you can use it in any of the other bundles at will:
namespace YourNameSpace\AnotherBundle\Controller;
use YourNameSpace\CoreBundle\Controller\CoreController;
class DefaultController extends CoreController
{//extends from your custom controller
}
And that's it: you're done.
First, don't name the class using underscores as in PSR-0 each underscore char is converted to a directory separator, when used in class name.
Second, put your controllers to <bundledir>/Controller/
Third, name your controller something like BaseController and extend all other controllers from it.
Fourth, think of using dependency injection rather than coupling functionality in a base controller.

symfony2 bundle class not getting autoloaded

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

Load models in Zend Framework

I tried to search here before creating this, but I couldn't find anything.
I have a simple project without modules and I'd like to load my models (which are inside application/models) without using any namespace and without usign any extra loading lines.
Basically what I want to do is to have my class Projects extends Zend_Db_Table_Abstract inside my models folder and to load it in my controller using $db = new Projects();
Is there anyway to do this? Is it recommended to use Model_Projects instead?
What If I had modules?
Edit:
I tried to use this without any other implementation and I got Class 'Projects' not found
It is because the Projects class is not autoloaded. Depending on your application namespace, (for example the default namespace 'Default') you have to name your class into something like: Default_Model_Projects

Categories