How to access Orchestra Xml Parser through Laravel IoC Container - php

I am using laravel 5.1. I want to use XML parser and I have searched and found Orchestra as mostly being used. So I have gone thorough all the steps given at documentation to install and configure. I have added Orchestra\Parser\XmlServiceProvider::class in providers section of config/app.php and 'XmlParser' => Orchestra\Parser\Xml\Facade::class in aliases section.
Now in my controller, I have added its name space like use Orchestra\Parser\Xml\Facade; at the top of my controller. But when i try to use its function in my action, like
$xml = XmlParser::load($xml_document);
It generates error stating,
Class 'App\Http\Controllers\XmlParser' not found
So I want to know is there any other way in Laravel 5.1 to use the packages and I am doing some thing wrong with Orchestra if some one has used it.

Since the documentation already describe registration of the facade alias:
'XmlParser' => Orchestra\Parser\Xml\Facade::class,
You can either use \XmlParser::load(), or import the alias.
use XmlParser;
or import the full namespace.
use Orchestra\Parser\Xml\Facade as XmlParser;

It looks as though it's searching inside the controllers for it..
Class 'App\Http\Controllers\XmlParser' not found
Therefore:
$xml = XmlParser::load($xml_document);
Needs to be:
$xml = \XmlParser::load($xml_document);
Should resolve this problem

In Laravel 5.1 the controller is in a namespace. The XmlParser is in an other namespace. You need to include that namespace in your controller.
<?php
namespace Orchestra\Parser\Xml; // Maybe this one is different
class Controller...
You can also add a \ to make it work.
$xml = XmlParser::load($xml_document);

Related

Use non-Laravel package in Laravel 7

I am trying to use this package within my Laravel project:
https://github.com/bkuhl/simple-ups
I am however struggling to use it within my controller as it has an hyphen within its path.
I have tried doing:
use Bkuhl\simple-ups\src\SimpleUPS\UPS.php
but it does not seem work. Does anyone have a guide as to how I can use it within my controller?
use SimpleUPS\UPS;
You can view the namespace of the class here
https://github.com/bkuhl/simple-ups/blob/master/src/SimpleUPS/UPS.php

Using multiple extensions (wrappers) with EasyAdminBundle (Wandi, AlterPHP...)

I would like to use the AlterPHP extension as well as the Wandi extension with EasyAdminBundle.
But we face some issue configuration both of them at the same time.
We used to have this config file when using only AlterPhp :
#routes/easy_admin.yaml
easy_admin_bundle:
resource: '#EasyAdminExtensionBundle/Controller/EasyAdminController.php'
prefix: /admin
type: annotation
And it was fine when we only used this bundle. However, now we want to use this bundle as well as the one quoted previously but it also needs to replace the easyadmin controller by the one from the new bundle.
So both extension wants to do the same thing and both extend the BaseAdminController from EasyAdmin.
What would be the best way to use both in the same project ?
I found a solution by making a custom controller that extends the AdminController from Wandi and copying the methods from the AdminController from Alterphp inside the custom controller. However, it seems like an odd solution to this problem.
I decided to contact both AlterPHP and Wandi on github and send a pull request on their extensions to use trait in their controller to make it easier to use multiple extensions.
So both of them answered to me :
Wandi reviewed my PR and merged it to master. It is now available in release 2.0.2.
AlterPHP reviewed my PR and merged it to master. It is now available in release 3.0.1
So with those changes it is way easier to use both extensions (and similar EasyAdminExtension) by using those new traits :
use Wandi\EasyAdminPlusBundle\Controller\AdminController as WandiController;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Wandi\EasyAdminPlusBundle\Controller\AdminControllerTrait as WandiTrait;
use AlterPHP\EasyAdminExtensionBundle\Controller\AdminExtensionControllerTrait as AlterPHPTrait;
class CustomAdminController extends EasyAdminController
{
use AlterPHPTrait, WandiTrait;
//You may have to solve conflict between those traits
}
You may have multiple problems such as services not known by the controller or methods defined multiple time.
I just had to redefine getSubscribedServices in my controller to add those used by AlterPHP and Wandi, as well as resolving a conflict with the method isActionAllowed defined in both traits.
use AlterPHP\EasyAdminExtensionBundle\Security\AdminAuthorizationChecker;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Wandi\EasyAdminPlusBundle\Controller\AdminControllerTrait as WandiTrait;
use AlterPHP\EasyAdminExtensionBundle\Controller\AdminExtensionControllerTrait as AlterPHPTrait;
use Wandi\EasyAdminPlusBundle\Exporter\Configuration\NormalizerConfigPass;
use Wandi\EasyAdminPlusBundle\Exporter\Configuration\PropertyConfigPass;
use Wandi\EasyAdminPlusBundle\Exporter\Configuration\TemplateConfigPass;
class CustomAdminController extends EasyAdminController
{
use AlterPHPTrait,WandiTrait { AlterPHPTrait::isActionAllowed insteadof WandiTrait; }
//It is important to set the subscribed services from the trait because they cannot use them otherwise.
public static function getSubscribedServices(): array
{
return \array_merge(parent::getSubscribedServices(), [
AdminAuthorizationChecker::class, //This one is for AlterPHP and those below for Wandi
'wandi.easy_admin_plus.exporter.configuration.normalizer_config_pass' => NormalizerConfigPass::class,
'wandi.easy_admin_plus.exporter.configuration.property_config_pass' => PropertyConfigPass::class,
'wandi.easy_admin_plus.exporter.configuration.template_config_pass' => TemplateConfigPass::class,
]);
}
}
I had to modify my services.yaml to be able to redefine getSubscribedServices for Wandi.
#services.yaml
services:
#...
Wandi\EasyAdminPlusBundle\Exporter\Configuration\NormalizerConfigPass: '#wandi.easy_admin_plus.exporter.configuration.normalizer_config_pass'
Wandi\EasyAdminPlusBundle\Exporter\Configuration\PropertyConfigPass: '#wandi.easy_admin_plus.exporter.configuration.property_config_pass'
Wandi\EasyAdminPlusBundle\Exporter\Configuration\TemplateConfigPass: '#wandi.easy_admin_plus.exporter.configuration.template_config_pass'

laravel 5 Class implements Iterator

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.

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()

CakePHP and namespaces?

Is there a way to put your own code into namespaces using cakephp? The following very simple controller class works fine.
class Customer extends \AppModel {
var $name = 'Customer';
}
However, if I add
namespace foo\bar;
cakephp can't find the controller anymore. Is there some way to tell cake in which namespace it should look for controllers?
I am using cakephp 1.3 and php 5.3.
I don't think there is. CakePHP looks for classes like PostsController or BlogController, not foo\bar\PostsController. Maybe you can tell CakePHP in what folder to look for those classes (probably), but then it will still be looking for unnamepsaced class names.
Why would you want this in a framework that doesn't use namespaces?
Why not give up the App::import() in cakephp 1.3. Replace it with the include_once().
I got my customize vendor classes defined under a namespace works fine. Just to prevent the collision of the custom class name with the official one.

Categories