I'm completely new to Symfony 2 and i can't really understand the folder structure and organization of a project. I know what a bundle is, but what is unclear to me is:
what's the main directory (the directory to be copied on the web server for deployment)
where assets (css, javascript) should be placed
if (and how) environment should be changed when publishing my website
I've already read the book on Symfony website and i can't find those answers.
The web root of a Symfony2 app is the 'web' directory, but when you push to production the entire symfony2 project should be pushed not just the web root.
From the symfony2 book
app/: This directory contains the application configuration;
src/: All the project PHP code is stored under this directory;
vendor/: Any vendor libraries are placed here by convention;
web/: This is the web root directory and contains any publicly accessible files;
The assets should be kept in the bundles 'Resources/public/[css/js/images]' folders. From here you would have to copy or symlink those directories into the web root to make them accessible. Symfony2 comes with a command line utility located in the 'app' directory. app/console assets:install web --symlink command executed from the command line of your symfony2 project root will install all of the bundle's assets for you.
To change environments and you are using Apache you would use a .htaccess file and mod_rewrite to select which environment you would want to use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
using /app.php puts me into production mode and using /app_dev.php would put me in development mode.
The main directory contains the app folder.
The assets should be
placed in the bundles folder they're related to. This folder is named
"public".
Read this. All you need to do is remove the app_dev.php file.
Related
I'm building my first Symfony site that will eventually be hosted on a shared server/cPanel site.
I'd like to have my Symfony/web content separate from the server files, though the way Symfony is structured, all the Symfony files are outside the public_html folder. But with a cPanel setup, there's already a lot of files and folders outside the public_html folder (mail, logs, .bashrc, .cpanel, www alias... and a dozen others).
I worry that it feels messy if I put Symfony mixed in with all these files and there could be a conflict. I would rather it be in a directory by itself. The best idea I've had so far is to make a subdomain to host Symfony where I can manually choose the web folder, and then just do redirects to the subdomain.
What do others do?
This is how I decided to do it, and it seems like it will work pretty well. I just use the regular cPanel public_html as the document root, upload the whole Symfony contents to that directory, but then add an .htaccess file with the contents:
RewriteEngine on
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
And that protects the main Symfony contents from public access while at the same time putting it all in a directory by itself. It seems to work so far. If anyone has a better idea, I'm open to it!
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.
My website is ready to be deployed and I am trying to set it up online.
Some informations:
The host is OVH.
It doesn't allow SSH, I have to send my files with FTP. No command line either.
I want to be able to set up the website in a subdirectory: /www/test for now (my current website is still in /www).
The problem:
When I open the URL my-website.com/test, a Symfony exception tells me No route found for "GET /test/", which clearly means that Symfony doesn't know it is in a sub-directory.
How can I tell it?
EDIT:
I just realized it worked when I access my-website.com/test/web.
Here I wrote exactly about that: https://www.refactory-project.com/install-symfony-app-in-a-subfolder-of-an-existing-site/
Upload the application part
Start by uploading the application folders at the same level of your site root:
[ftproot]
-- public_html
---- ...
---- ...
-- symfonyapp
---- app
---- bin
---- src
---- vendor
---- web
------ app.php
------ app_dev.php
------ ...
---- composer.json
---- composer.lock
Move the web part
Move the content of the "web" folder into the desired subfolder, i.e. "myapp".
[ftproot]
-- public_html
---- ...
---- ...
---- myapp
------ app.php
------ app_dev.php
------ ...
-- symfonyapp
---- app
---- bin
---- src
---- vendor
---- composer.json
---- composer.lock
Let the web know where is the application
Edit files app.php and app_dev.php and insert the new application location.
require_once __DIR__ . '/../../symfonyapp/app/bootstrap.php.cache';
require_once __DIR__ . '/../../symfonyapp/app/AppKernel.php';
Let the application know how the web folder is called
Edit file composer.json with the new web folder name
{
...
"extra": {
...
"symfony-web-dir": "../public_html/myapp"
}
}
You're sort of setting yourself up for hardship if you are deploying a Symfony app to a host that does not allow SSH - for instance if you want to rebuild your cache you will have to manually nuke your app/cache/* dirs?
To answer your question directly, if your Symfony project is in /www/test/ then Symfony's web directory is /www/test/web so you need to use a url like:
http://foo.com/test/web
For completeness, try accessing the explicit url - /test/app.php and /test/app_dev.php respectively. If you receive a Symfony error page in either case you know you are on the right path at least.
Edit #1
Something to point out: your project and configuration files and may be readable with this deployment scenario - which is not ideal - so you might want to check this and take some actions to secure this directory if possible. I appreciate that this is probably a test deployment so it might not be a big deal, but it's always good to keep mindful of security :)
Edit #2
Okay YMMV with this, I am no .htaccess expert but you could deploy your symfony app to /www/symfony/ and rewrite the /test URI to show /symfony/web/ instead; e.g:
RewriteEngine On
# rewrite all `/test/*` uris to `symfony/web`
RewriteRule ^test(.*)$ symfony/web/$1 [L,QSA]
# direct access to /symfony dir is a 404
RewriteRule !^symfony/web/$1 - [R=404]
This should serve all applicable uri requests (to /test and /symfony/web itself) to Symfony, while restricting direct access to the symfony core files.
Haven't tested this, so how this will play with Symfony's own .htaccess is not something I can answer off the top of my head.
How about using .htaccessfile to achieve your goal. As far as I understand your problem your symfony app works IF you access it via web folder for example foo.com/test/web
try using the following code in your .htaccess file which will sit at the root of your test directory
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
</IfModule>
please replace the domain.com with your domain name.
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.
The symfony is in /Frameworks folder, and the root path is in Apache2/htdocs/, how to deploy a symfony project? Do I need to copy symfony folder to htdocs, condisdering Apache2/htdocs/ like localhost/?
You can copy your Symfony folders in the htdocs folder and set Apache to serve from Symfony's 'web' folder which contains the app.php file.
You need to do some Apache configuration here, you can check this post