Adding a custom lib library to Symfony2 startup

I tried this for several hours - and I can not find any messages that work. I am adding custom php classes to the Symfony2 vendor directory.

For example (copied another provider structure):

provider /MyLib/MyLib/SRC/MYLIB/Lib/Class.php

Then I updated the root composer.json file by adding:

"require": { "mylib/mylib": "@dev" }, 

I also created a .json composer in vendor / mylib / mylib that contained:

 { "name": "mylib/mylib", "type": "library", "description": "My Libraty", "keywords": ["library"], "autoload": { "psr-0": { "Mylib\\": "src/" } }, "minimum-stability": "dev" } 

I added a namespace in Class.php:

 namespace MyLib\Lib; 

In one of my packages, I added the following:

 use MyLib\Lib\ClassName as ClassName; class Cms extends ClassName {} 

The error I get is:

FatalErrorException: Error: class 'MyLib \ Lib \ MyClass' was not found in C: \ xampp \ htdocs \ My_CMS \ src \ Cms \ CmsBundle \ Entity \ Cms.php line 13

What am I doing wrong?

+8
php symfony composer-php
source share
4 answers

In php, classes are automatically loaded via __ autoload

Symfony will take care of this Class Loader , but it actually works the same. There is no background scan of all directories, so you need to add your namespace manually.

You need to add the autoload.php file:

 $loader->add('NAMESPACE','/path/to/vendor'); 
+4
source share

You really need to use service containers, which are basically symfony's way of loading classes.

That's why:

  • Namely, the service is never built until it is needed.
  • Best practice for code reuse.
  • Separating every functionality of your application.
  • Since each service performs only one task, you can easily access each service and use its functionality wherever you need.
  • Each service can also be more easily tested and configured since it is separated from other functions of your application.
  • This idea is called service-oriented architecture and is not unique to Symfony or even to PHP.

http://symfony.com/doc/current/book/service_container.html

 A Service Container (or dependency injection container) is simply a PHP object that manages the instantiation of services (ie objects). 

That way, basically symfony will process the class instances for you in your controller. All you have to do is the following:

Add a folder under your path called Libraries โ†’ src / AppBundle / Libraries

Add a class to this folder with the namespace at the top. My example:

  <?php namespace AppBundle\Libraries; class MyRecommendations{ public function __construct(){ } public function init(){ die("init"); } } 

Then add a file called services.yml to your application. application /Config/services.yml

Put the following code in it, don't use tabs in yml file

 services: myrecommendations: class: AppBundle\Libraries\MyRecommendations #arguments: [specialparamhere] #constructor parameters here 

Then add the resources of the third line: services.yml to the config.yml file.

 imports: - { resource: parameters.yml } - { resource: security.yml } - { resource: services.yml } 

At the top of your controller, when using, simply add a usage statement

 use AppBundle\Libraries\MyRecommendations; 

Now call your code

 $test = $this->get('myrecommendations'); $test->init(); 

echo out

 init 
+9
source share

If you have another project that uses the autoloader to load its classes, such as SimpleSAML, and wants to use it in a class in a different namespace:

 namespace Study\UserBundle\Service; require_once "/full_path/simplesaml/lib/_autoload.php"; use SimpleSAML_Configuration; use SimpleSAML_Auth_Simple; use SimpleSAML_Auth_State; class SimpleSAMLLogin { 
0
source share

The last answer is correct: https://stackoverflow.com/a/166778/

I work for me, however itโ€™s important to add that this is correct, but if you are before setting up the en autoload library. In my case, in Symfony 3.2.x, I added my en composer.json library:

 "autoload": { "psr-4": { "": "src/" }, "classmap": [ "app/AppKernel.php", "app/AppCache.php","**src/AppBundle/juanitourquiza/pagopayphone/library"** ] 

And after this instruction:

 composer update 

Then everything is fine.

Hi

0
source share

All Articles