Autoload Bundle with custom path in Symfony2 - symfony

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.

Related

Symfony bundles using the same vendor name in /src and /vendor

I'm breaking some of my bundles into stand alone repos that I can reuse in other projects, they are all under the same vendor namespace in my /src directory, so when I pull them out and require them with composer I end up with the same vendor name space in my /src directory and also the /vendor directory.
When I try to load my app I get a Compile Error as it's looking for the class in the /src directory.
Is there anyway around this or do I need to change my vendor name?
Turns out the opcache needed to be cleared - restarting apache solved the issue.

Symfony2 - Target directory changed when generating a bundle

I've noticed this recently, from a clean install of Symfony2 and removed AcmeDemoBundle (from the AppKernel, routing_dev.yml etc.). I generate the bundle and noticed that the Target directory for the bundle has changed and every time I create a new bundle I have to manually put in the proper path to it. Seems to be a problem with the latest release of Symfony2.
Changed Target directory line during bundle creation:
Target directory [/var/www/html/Project/app/cache/dev/../src]:
The path that it use to point was:
Target directory [/var/www/html/Project/src]:
I am at a loss for what changed as I installed a clean build and did not alter any settings.
When I installed Symfony2 I used the following: (which is the same way I've used previously)
composer create-project symfony/framework-standard-edition Project #stable
php app/console generate:bundle (see last line)
Welcome to the Symfony2 bundle generator
Your application code must be written in bundles. This command helps
you generate them easily.
Each bundle is hosted under a namespace (like Acme/Bundle/BlogBundle).
The namespace should begin with a "vendor" name like your company name, your
project name, or your client name, followed by one or more optional category
sub-namespaces, and it should end with the bundle name itself
(which must have Bundle as a suffix).
See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more
details on bundle naming conventions.
Use / instead of \ for the namespace delimiter to avoid any problem.
Bundle namespace: Foo/FooBundle
In your code, a bundle is often referenced by its name. It can be the
concatenation of all namespace parts but it's really up to you to come
up with a unique name (a good practice is to start with the vendor name).
Based on the namespace, we suggest FooFooBundle.
Bundle name [FooFooBundle]:
The bundle can be generated anywhere. The suggested default directory uses
the standard conventions.
Target directory [/var/www/html/Project/app/cache/dev/../src]:
Fixed by updating composer:
composer self-update
Then creating a new project.

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.2 Deployment Questions

Because of an other problem on my server, I tried to setup a real new project and did this (localhost with xampp on windows):
installed curl
loader composer like told in docs
loader vendors like told in docs
merged app/config-folder
copied src-folder
copied web/bundles/myBundle-folder
After that, I tried the deployment-steps documentation. I skipped uploading the files and editing parameter.yml (it's already configured for localhost).
Than I am running into trouble doing 'B) Update your vendors':
php composer.phar install --optimize-autoloader
This steps remove all my files from the web\bundles\myBundle-folder.
Do I have to tell composer about my bundle? I first thought, that I have to store them somewhere else, but this thread tells me, that I am right?!
The thread you are mentioning is from symfony 1, but you are installing a symfony 2 application.
In web/bundles are your assets installed (css, javascript and images).
Your bundles which will be installed via composer are in your vendors folder.
And your own bundles should be in the src folder.
I think this is a good start to have a look at where to store your files.
Your css, js and images should be stored in a bundle, e.g.:
src/AcmeDemoBundle/Resources/public/css
src/AcmeDemoBundle/Resources/public/js
src/AcmeDemoBundle/Resources/public/images
And then you can install your assets with php app/console assets:install web and they will be pushed into the web/bundles folder.

Symfony 2, adding vendor library (PHPExcel etc.)

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.

Resources