Symfony 2, adding vendor library (PHPExcel etc.) - symfony

In my symfony 2.2 app I wanted to use PHPExcel library. So I downloaded it, and copied contents of Classes library to /vendor/phpexcel directory:
vendor/
phpexcel/
PHPExcel/
PHPExcel.php
After that I added the following to app/autoload.php directly below $loader = require ... line:
$loader = require __DIR__.'/../vendor/autoload.php';
//The following was added
$loader->registerPrefixes(array(
'PHPExcel' => __DIR__ . '/../vendor/phpexcel'
));
// intl
...
Now if I browse to my web app, it returns HTTP Error 500 (Internal Server Error). I read the following post, but wasn't able to solve the problem:
How to use PHPExcel correctly with Symfony 2
Can someone help me correct this?

You should never manually download something and put it in the vendor directory. Composer manages the vendor directory, and hence it should be save to delete this directory and run composer install again. The vendor directory also is excluded from Git by default.
To install PHPExcel using composer, add it to composer.json:
"require": {
...
"phpexcel/phpexcel": "1.7.*"
}
When installed with Composer, you should not need to worry about the autoloading either.

I installed https://github.com/liuggio/ExcelBundle for PHPExcel. Bundle includes PHPExcel (addes related links to composer). You can easily use PHPExcel without wondering what the bundle says. Call new \PHPExcel(); then you move. I hope this bundle helps.

Composer seems to have a problem with SELinux. See this. Though not recommended, setting SELinux to permissive can be a workaraound.

Related

Magento 2.0 Unknown module(s): error when enabling custom module from command line

I've read Magento's DevDocs and Googled this problem but the usual missing registration.php answer doesn't apply here.
I'm on the released CE 2.0.0 version and I'm simply trying to enable my first minimum test module in magento/vendor/ but
bin/magento module:enable -c Tchsl_Test
results in:
Unknown module(s): 'Tchsl_Test'
I am basing this on the naming conventions and file positions of modules in vendor/magento/
In vendor/tchsl/module-test/etc/module.xml I have
<config xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Tchsl_Test" />
</config>
In vendor/tchsl/module-test/registration.php I have
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Tchsl_Test',
__DIR__
);
What am I missing?
I had this same problem and after some tinkering, discovered that our Modules actually belong under app/code/Tchsl/Test/. Move your module files there, and running the shell command module:status should show your disabled module.
You don't need to put your module under app/code/. In app/code/ Magento2 will search and find your module's registration.php. In Composer's vendor/ dir it doesn't do that, so we need to trick Composer into loading your module's registration.php.
If you'd check any Magento2 module's composer.json in vendor/magento/module-*, you'll see an "autoload" section which references the registration.php file. So Composer will autoload your module's registration.php which will "tell" Magento2 where your module is located.
This is a fragment from the Magento Checkout module's composer.json:
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\Checkout\\": ""
}
}
So if you have your module in a separate repository and loaded via composer, then that is the way to go. If you do not have it in a separate repository, then your module does not belong on vendor/ but in app/code/.
See also this Magento Stack Exchange post.

Autoload Bundle with custom path in Symfony2

I am developing a Symfony2 web app that will be hosted on an Ubuntu server and want my directory structure to be:
/Symfony (with an app,bin and vendor folder in it)
/Site1
/Site2
This way site1 and site2 use the same Symfony framework. In /Site1 I want:
/web
and in there I want app.php and app_dev.php as well as /web/AppBundle with all of the bundle files. I already figured out that I need to change in app.php require_once DIR.'/../Symfony/app/AppKernel.php'; in 2 places as per http://symfony.com/doc/current/cookbook/configuration/override_dir_structure.html
When I generated the bundle I did get the message "Checking that the bundle is autoloaded: FAILED" and that I should "Edit the composer.json file and register the bundle namespace in the "autoload" section"
I added "AppBundle": "/web/AppBundle" to the composer.json autoload section and in cygwin I run "composer -n install" It starts with saying that it is loading composer repositories and then it says "Installing dependencies (including require-dev)" and never goes past that.
What can I do to register this bundle that is not in the standard folder so that autoload picks it up? I am just getting the fact that the class could not be found in AppKernel.php
Thanks.
It's simple. Just don't do it! Use symfony as it is intended with its default directory structure.
Great two projects. If you have common code, extract it to an extra bundle and install it in both projects via composer.

Symfony 2 - How to determine the namespace and Bundle name for autoload.php & Appkernel.php

I'm very new to symfony2 and I cannot find this info:
To register a bundle located in my vendor dir, how to determine the namespace and Bundle name for autoload.php & Appkernel.php?
For example, I have downloaded Luiggio's PHPExcel Bundle. I have place it in vendorDir/ExcelBundle/
Where content is:
namespace Liuggio\ExcelBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class LiuggioExcelBundle extends Bundle
{
}
What lines should I put in Appkernel and namespace.php?
This and this does not work:
new Liuggio\ExcelBundle\LiuggioExcelBundle()
//'Liuggio\\ExcelBundle' => array($vendorDir. '/PHPExcel'),
I cannot use composer or github repo at all, too many proxies and restrictions where I am.
You shouldn't place bundles in your vendor directory manually. Let Composer do this for you. Not only does Composer know where vendor libraries / bundles should be located, it also adds them to your autoload files and performs some other automated tasks.
To tell Composer which libraries are required, you should add them to your composer.json:
"require" : {
(...)
"liuggio/ExcelBundle": "~2.0"
},
Next, using the command line, run the composer update command:
$ php composer.phar update
(if you don't have a file composer.phar in your project directory, but you have Composer installed globally instead, use the following:)
$ composer update
This will tell Composer to download the required dependencies, update the autoload script, etc, all automatically. When it's finished, you're ready to go.
If you can't use Composer on your server, then run it locally before you upload your files (although I strongly recommend moving to a server that does allow you to use Composer).
The line you're trying to add to AppKernel.php is correct, however it only works after running Composer (or you'd indeed have to download the files and update the autoloader manually, but I'd strongly recommend against that).
Edit
If you really can't use Composer, do the following:
Place the files of ExcelBundle in the following directory:
vendor/liuggio/ExcelBundle/Liuggio/ExcelBundle
Your line in AppKernel.php was already correct.
Add this line to autoload_namespaces.php:
'Liuggio\\ExcelBundle' => array($vendorDir . '/liuggio/ExcelBundle'),
Last but not least, complain to your system administrator that he's making your work impossible with his stupid security measures.
new Liuggio\ExcelBundle\LiuggioExcelBundle(),
in AppKernel should be working fine.
This is the namespace of the LuiggioExcelBundle class + the class name. Look how your bundles are loaded, its the same.
What's your error?
You say vendorDir/ExcelBundle/ but its vendor/ExcelBundle right?
And what do you mean by namespace.php? :o
https://github.com/liuggio/ExcelBundle ==> there readme is rly easy to understand, it should help you.
For your "proxies and restriction", composer is a powerful tool, I can help you to use it. Download this soft, the free version is enough http://www.frozenway.com/ (if you cant read french, the 1st input in the header is to translate the website, English is Anglais) With this, you wont have any ports restriction.

Symfony 2.1 own vendor library

I want to put some code that I shall use in different projects outside the app's bundles. I put it in a new folder in the vendor folder but I can't included in the project. Which are the steps?
Should I follow a special structure of folder and namespaces
How do I included in my project.
On Google I found a lot of ambiguous answers. I tried also this but it did not work.
You need to get your library loaded. You do this by telling composer where to find your library.
The best thing would be to set up a git repository for this source code and then use composer to load it, keep it up to date and include it into the autoloader. You may need to get used to composer to do this, but as composer is getting the default dependency management tool for php, the time is not wasted. You can find the docs here: http://getcomposer.org/doc/
If you don't want to do this, you can add a new src code folder to composer. Let's say your folder structure is like this:
symfony2/
vendor/
yourlibrary/
Bla/
Blub/
MyClass.php
First, the MyClass.php should have the namespace Bla/Blub defined. Else Then you can add your library like this to the composer.json file:
"autoload": {
"psr-0": {
"": "src/",
"Bla": "vendor/yourlibrary/"
}
}
You already have autoload defined, so you need to overwrite it!
If your library doesn't have namespaces, you can define composer to load it nonetheless. I am not using this, so I can just link the docs where you can read it up: http://getcomposer.org/doc/04-schema.md#autoload

Symfony2: How to update a bundle whose source files have been modified?

I am using the KNP Pagination Bundle. I customized the twig file in the bundle source. Then I found a better way of doing it without touching the bundle's files.
Unfortunately, now everytime that I do
bin/vendors install
I get the following error:-
"KNP Paginator Bundle" has local modifications. Please revert or commit/push before running this command again.
My .gitignore file has ignored /vendors
And my deps file has the bundle included too.
Is there a way to uninstall a bundle? So that I can reinstall it?
Or what is the best way to solve my problem?
./bin/vendors doesn't care about content of .gitignore. You can fork desired bundle, do your changes there and change deps file to point to your fork instead.
If you still want to use original bundle and just reinstall it, you can either run ./bin/vendors install --reinstall or just delete the bundle folder from vendor directory and run ./bin/vendors install again.
How about using git --reset? The vendors are fetched using git clone after all.
Can you explain what "git reset" does in plain english?

Resources