creating my own library in symfony 2 - php

I'm trying to create my own library in a Symfony2 project but I'm having a hard time doing so...
Basically I want to reuse an FTP browser class I made for another project.
I copied the class into
/vendor/mylib
and tried to autoload it like that
$loader->registerPrefixes(array(
'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
'Twig_' => __DIR__.'/../vendor/twig/lib',
'Mylib_' => __DIR__.'/../vendor/mylib'
));
I then tried to instantiate a Mylib_Test object inside my bundle's controller and I got this error :
Fatal error: Class 'Test\FrontBundle\Controller\Mylib_Test' not found in /Applications/MAMP/htdocs/sf2_project/src/Test/FrontBundle/Controller/WelcomeController.php on line 26
Anyone has an idea on how to do this ?

You probably have namespace Test\FrontBundle\Controller; in your controller. When you call for some class php tries to find it in specified namespace. Mylib_Test is obviously not in that namespace. So you should implicitly indicate that this class should be looked for in global namespace. In order to do that you should prepend class name with backslash:
$instance = new \Mylib_Test();
More info in docs

Related

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.

getting error when adding a class in codeigniter

I am using a Facedtector class for my work. I added the class in my Codeigniter libraries folder and just called from my controller class. In my controller I also loaded the class but run time it is showing following error:
An Error Was Encountered
Non-existent class: FaceDetector
Please anyone can help me about this.
Thanks
Add your class file inside you library folder of your main application folder.
And then you can use the auto load function of codeigniter
application/config/autoload.php
at the above location and then you can access the class directly or other way of using this use
$this->load->library('Your_Class_Name', 'Path\to\your\class');
This way you can access your class in codeigniter
You can follow below link for autoload of a class library.
https://ellislab.com/codeigniter/user-guide/general/autoloader.html

zf2 view helper across module

I created a helper in the application module, and it works perfectly. When I try to load it from another modules, such as user, it tells me that it can not find the class.
Class 'Application \ View \ Helper \ Footertable' not found
I tried to put this code in module.config.php well as the application module even in the same file of the user module.
'view_helpers' => array (
'invokables' => array (
'footertable' => 'Application\View\Helper\Footertable'
)
),
I think it's a problem autoloading class but I can not find information on how to load this helper when you are in another module
I invoke helper in view file using
$this->footertable()
work perfectly in application module but not in user module
any idea?
Hello,
but my code is correct
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Footertable extends AbstractHelper{
protected $inizioFine;
protected $numero;
public function __invoke($inizioFine,$numero){
$this->inizioFine = $inizioFine;
$this->numero = $numero;
echo sprintf('Mostra %d a %d di %d record',$this->inizioFine['start'],$this->inizioFine['end'],$this->numero);
}
}
the space in config is an copy & past errors.
I still have the same problem: can't load helper from another module
update full error
Fatal error: Class 'Application\View\Helper\Footertable' not found in D:\www\httpdocs\test\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 170
path is
D:\www\httpdocs\test\module\Application\src\View\Helper\Footertable.php
The path you posted doesn't look right. All the files in src for your Application module should be inside a folder called Application, since that's your top level namespace. So the path should be:
D:\www\httpdocs\test\module\Application\src\Application\View\Helper\Footertable.php
That would explain why the helper can't be autoloaded, but I don't understand how it works in the application module if this is the case.
Your configuration seems good. Probably the problem is in your helper class signature.
Since PhpRenderer composes a HelperPluginManager instance to manage helpers, your helper should implement the HelperInterface (1) to be registered correctly. Also you should write an __invoke() method within your helper to invoke it by method call. (2)
So, in your Footertable class, simply extend the AbstractHelper and make sure you have an _invoke() method. This is recommended way to write custom view helpers in documentation.
For example:
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Footertable extends AbstractHelper
{
public function __invoke()
{
return 'bar';
}
}
And use it in your views like this:
echo $this->footertable();
It should work.
Note: You also have to register all helpers (and other classes) in your module configuration's invokables section without spaces between the backslashes:
Wrong:
'footertable' => 'Application \ View \ Helper \ Footertable'
Correct:
'footertable' => 'Application\View\Helper\Footertable'

Why can't symfony find my auto-loaded class?

I'm trying to use the php-ga third-party library in my symfony project. I've installed the library in apps/<app>/lib, cleared the cache, and the third-party files are appearing as I would expect in config_autoload.yml.php:
'tracker' => 'C:/wamp/www/apps/api/lib/php-ga/GoogleAnalytics/Tracker.php',
'transaction' => 'C:/wamp/www/apps/api/lib/php-ga/GoogleAnalytics/Transaction.php',
...
However, when I try to use the classes in an action under the same app like so:
use UnitedPrototype\GoogleAnalytics;
public function executeNew(sfWebRequest $request)
{
$tracker = new GoogleAnalytics\Tracker(...);
...
I get an error saying it can't resolve the class:
Fatal error: Class 'UnitedPrototype\GoogleAnalytics\Tracker' not found in C:\wamp\www\apps\api\modules\encoding\actions\actions.class.php
What am I missing?
Symfony's autoloader can not load namespaced classes properly. I think you should move the ga lib to lib/vendor/php-ga and use it's own autoloader in config/ProjectConfiguration.class.php (like require_once __DIR__ . '/../lib/vendor/php-ga/autoload.php').

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

Categories