Symfony2 moving to demo bundle over to production - php

I am going thorough the quick start for Symfony 2 and I'm getting quite confused. I have the following directory structure after unpacking Symfony2 (As in the documentation):
/var/www/ <- web root directory
Symfony/ <- unpacked archive
app/
cache/
config/
logs/
Resources/
bin/
src/
Acme/
DemoBundle/
Controller/
Resources/
...
vendor/
symfony/
doctrine/
...
web/
app.php
...
I've got the demo working at <host>/Symfony2/web/app_dev.php/demo/welcome/James. So far so good.
I know I can't use apache's mod_rewrite with app_dev.php so I was wondering if someone could give me a step by step to:
Moving the DemoBundle over to using app.php (because <host>/Symfony2/web/app.php/demo/hello/James is not working. It's throwing up a Symfony error. I swear I haven't touched a thing. I'm at the bottom of the first page of the tutorial)
Keeping the development toolbar when using app.php (do I just cut web_profiler: from config/config_dev.yml?)
Setting up Symfony2 outside of the web route (best thing for security I presume?!?) (Should I have to have a soft link /var/www/index.php pointing to /home/<name>/Symfony2/web/app.php?)
Now I'm on the production version will I be able to go to <host>/demo/welcome/James, if not, why not? (I have apache's mod_rewrite installed) (I'd really like to get this bit working)
(I had the same problem learning django, just to much information to absorb at first.)

The demo is not meant to be run in production. However, if you wish to do so for learning purposes, simply move the routes _demo and _demo_secured from app/config/routing_dev.yml to app/config/routing.yml.
Using the web profiler in production is again not sensible. Site visitors must not get access to such information. This is the reason why there are two separate front controllers (app.php and app_dev.php). They've both set up for different purposes.
The best practice in setting up the web server would be to have the web root point to ./web.

Related

Twig 1.x - what files can I safely remove for production?

Following an audit, I've been tasked to remove extraneous files from the Twig 1.x vendor directory in one of our sites. Planning on removing /twig/twig/doc, /twig/twig/test and see if anything breaks.
What about /twig/twig/ext/twig/run-tests.php, or the entire "ext" directory?
Does anyone have prior experience weeding a default Twig 1.x installation for production environments? Any assistance or advice gratefully welcomed.
The correct action would be to update your site's layout so that these files are outside your web server's document root -- then you don't have to worry about what to delete and what to leave. You probably have something like this, where your web server's document root is pointing directly at /path/to/project:
/path/to/project
/lib
foo.php
bar.php
/twig
/twig
/doc
/test
index.php
This means anybody can directly request http://yourdomain.com/twig/twig/test/some_file.php
What you want is more like this:
/path/to/project
/public
index.php
/lib
foo.php
bar.php
/twig
/twig
/doc
/test
Then configure your web server so that its document root is /path/to/project/public. Then your application code can still include() things in /twig and /lib, but your web server won't directly serve them.
If your removing files from the vendor directory they'll come back the next time you do a composer install so this seems kind of pointless.
The files you mention (docs and test) are causing no harm other than taking up space as they are not directly called, but that's just a downside to any package management system. Like #Alex said as long as they are not publicly accessible there is no need to worry.
If you really want Twig without the extra files you could fork the project, move it into your own Git repo then reference that in your composer.json instead of the official one - but you will miss out on any updates from Twig.
We're running on Apache, so the easiest solution was to simply add a .htaccess file to the top vendor directory:
# Prevent non-local access to the vendor directory.
Order deny,allow
Deny from all
Directory structure:
/root
/vendor
.htaccess
/twig
...
/includes
...
Now the PHP scripts continue to have access, but external attempts to view anything inside the vendor directory return a 403 error.

Symfony3 routing not working with web root configuration

I am trying to integrate some new Symfony3 apps into an existing web space. At my webroot /html, each app has it's own directory. Each of these apps could be anything- cakePHP, custom PHP, whatever. And each are accessed by a URL like localhost/appname. This structure is not flexible and I am not able to add anymore URL patterns to vhosts or anything like that.
Therefore, I have my Symfony3 install at /symfony which is a sibling directory of /html. Inside of /html I have a landing directory for my Symfony app: /html/symfonyapp.
In /html/symfonyapp/index.php I have one sole line of code:
require_once DIR.'/../../symfony/web/app.php';
In my Symfony set up, I have a bundle called SymfonyappBundle. I have a route configured in src/SymfonyappBundle/Resources/config/routing.yml to redirect calls to /symfonyapp to this particular Bundle.
The routing is not working. Calls to http://localhost/symfonyapp always end up going to the routing for "/" Why? I feel that it has nothing to do with my Symfony setup, but instead something to do with the request coming in through that /html/Symfonyapp/index.php file.
Any help is greatly appreciated!
Edit: I see it's helpful to list out the directory structure so here it is:
- /var/www/html <-- this is your (global) web root
|- cake-app
|- custom-php-app
|-symfonyapp
|—index.php (which contains only a require for app.php)
-/var/www/symfony <—symphony standard install here
|- app/
|- vendor/
|- src/
|- web/ <-- the web root for your symfony-app
|- .htaccess
|- app.php <-- the "boot"-script similar to index.php
I hope I got this right. Your directory structure looks something like this:
- /var/www/html <-- this is your (global) web root
|- cake-app
|- custom-php-app
|- symfony <-- the project root for you symfony-app
|- app/
|- vendor/
|- src/
|- web/ <-- the web root for your symfony-app
|- .htaccess
|- app.php <-- the "boot"-script similar to index.php
So basically you have a typical Symfony-application, but it sits inside a shared web root. So when accessing it you don't go to http://example.com/, but instead to http://example.com/symfony/web/
This might be the first problem you are having. Your application must be accessed from the web folder, not from the symfony-folder. Depending on for example some rewrite rules in /var/www/html/.htaccess you might not be able to look through files in the symfony-subfolder and there is no entry script, so it will not work. Dependening on your setup you might not even have permission to rewrite the url per .htaccess or in your server's config, this would complicate things a bit further. For now let's assume the .htaccess-file in web/ does work and it's just a matter of the wrong folder your url is pointing at.
There are multiple options you have if you want the url to be accessible at http://example.com/symfony/ (without the web/-part). Symfony's project structure is actually pretty flexible and you could get rid of the symfony/web/-folder and instead use symfony/ as your web root. There might be some gotchas for example with some install scripts that copy resources like css and js into your web-root. You could also run into issues when bundles point to the web-directory, e.g. for storing uploads. You probably have to make a few adjustments but a basic setup should be doable in no time by moving all files from web to the parent folder (including the .htaccess which might be hidden).
Another option might be to create a new .htaccess in symfony's project root that points to web/app.php instead of just app.php. You could take the existing file as a reference. I try to avoid using htaccess-files and don't have a setup right now were I could try it, but it might be worth a shot before moving lots of files around. Although you still might run into issues with assets where the path is not matched correctly.
edit: another option that's probably more work, but might be useful if you want to migrate away from the other existing web apps to just a Symfony app is, to move symfony to the same level as html/ and move all the stuff from web/ into html/. Now your server's web root is also symfony's web root (again you might have to fiddle around with assets expecting to be in a folder called web/). Now you just need to make sure that whenever Symfony does not find a route it will pass the request to your other apps. There are several things to look out for and it's a lot more work than the 2 approaches above, but in can be useful. There was a pretty good talk about it at last year's SymfonyCon in Berlin on how to do this if you are interested in this route:
https://github.com/SymfonyCon/2016-talks#modernizing-with-symfony
https://slidr.io/derrabus/modernizing-with-symfony
Unfortunately the video of the talk is not out yet.
The solution that I found was to add a custom Kernel for each web application. So for each folder I have under my webroot /var/www/html, I added a new Kernel in Symfony. Each Kernel has it's own routing and config files, which solved my routing issues! I followed this symfony doc to set up the kernels: http://symfony.com/doc/current/configuration/multiple_kernels.html
App1:
webroot: /var/www/html/app1/index.php has the typical app.php code in it which initializes App1 Kernel as such: $kernel = new App1Kernel('dev', true);
symfony details: in app/config I added one directory per Kernel , so this would be app/config/app1. I copied all config, routing, service files into this directory and reference the custom Bundle for the app.
App2:
webroot: /var/www/html/app2/index.php has the typical app.php code in it which initializes App1 Kernel as such: $kernel = new App2Kernel('dev', true);
symfony details: in app/config I added one directory per Kernel , so this would be app/config/app2. I copied all config, routing, service files into this directory and reference the custom Bundle for the app and changed any references to Bundles to the appropriate bundle.

How to copy images to web folder with assetic

I can't believe that I'm not able to google this...
So, I have a symfony2 application, and I installed jQuery UI with Composer. That means i have project structure like this:
/app/
/src/
/vendor/components/jqueryui/
/web/
I have assetic set up to copy js/css files from the vendor folder to web. That works fine. Config:
assetic:
assets:
javascripts:
inputs:
- %kernel.root_dir%/../vendor/components/jquery/jquery.js
- %kernel.root_dir%/../vendor/components/jqueryui/ui/jquery-ui.js
- %kernel.root_dir%/../vendor/components/jqueryui/ui/i18n/jquery.ui.datepicker-cs.js
stylesheets:
inputs:
- %kernel.root_dir%/../vendor/components/jqueryui/themes/redmond/jquery-ui.css
filters: cssrewrite
After assetic:dump i have
/web/js
/web/css
cssrewrite rewrites images/image.png to ../images/image.png. I guess that's ok, assumes a /web/images directory with said images.
Now the question is: How do I copy the images from /vendor/components/jqueryui/themes/redmond/images to /web/images?
Or is there another best practice to do that? Excuse me if it's and obvious thing, I'm a .NET guy, just trying out PHP.
My answer probably isn't spot-on because I haven't tried this with vendor files but this is what worked for me:
app/console assets:install
It copies all of your public resources to your web directory.
This answer by user1814739 helped me understand this and may provide additional information for you.
Also consider this advice from the Symfony Blog:
Although developers usually execute the command without any option, most of the time it's better to execute it with the --symlink option. This makes a symbolic link of your assets instead of actually copying their files. This means that any change in the content of the web assets will have immediate effect in the application.

Hosting PHP Zend Framework application on shared cPanel server

I'm trying to deploy an PHP application which is written with Zend Framework to a shared cPanel server.
There are not many tutorials available on this area online, however, I followed several of them. It is successful to run the test page which proves the zend framework is installed correctly.
However, since cPanel server has a default root directory called public_html/, it is impossible to simply rename it to the Zend Server's default public/.
As a result, I had two options in mind: (Say the project name is AAA)
1) upload my projects under the /public_html/ directory, then the project will be like /public_html/AAA/public, and etc.
However, this one simply fails to work.
My thought would be something wrong here with the baseUrl setting, however, no matter I comment ( which is to remove the baseUrl) or set to the root page, ( in this case /public_html/AAA) both failed.
2) I tried to follow the way listed in this article: http://blog.motane.lu/2009/11/24/zend-framework-and-web-hosting-services/. Still failed.
Can anyone suggest how to do it?
Really appreciate your help!
Just symlink it:
ln -s public public_html
then this structure will work:
htdocs/
myvhost.com/
public/
application/
library/
public_html # this is actually a symlink pointing to public
Whatever you do, dont just throw everything in the publicly accessible area... its just bad form :-)
I don't think ZF cares what you name your "public" directory. It's just the convention that's typically used.
I can't think of any ZF component or common use case where anything explicitly points at "public/...".
A project structure like this should work:
myproject/
application/
library/
public_html/ # this used to be public until you renamed it.

cakephp routing problem (or maybe just confusion)

I've just started learning cakephp and have gotten the Blog example working except for the routing, I'm still not quite sure how it works after reading many, many documents on routing (including the ones in the official cookbook).
My problem is with the '/' root routing, I want it to go to the index() function of the PostsController so I use:
Router::connect ('/', array('controller'=>'posts', 'action'=>'index'));
But this doesn't work if I go to the url: localhost/
This is probably (most definetely) because I don't know where to put the cake_1_3 folder/installation, currently my directory tree for localhost (in htdocs) looks as follows:
-htdocs>posts>cake_1_3
This means that when I navigate to: localhost/ I get nothing and when I navigate to: localhost/posts/ I get nothing, just the directory listing for the folder "posts" which shows I have the directory "cake_1_3".
It is only when I go to the url: localhost/posts/cake_1_3/posts/ does the routing work, as in it sees the second "posts" and so runs the "index" function of "PostsController".
Obviously this isn't what I want, I want to be able to go to: localhost/posts/ and it use the index function of the PostsController.
Update: I actually tried taking all the cakephp stuff out of "cake_1_3" and just into "posts" but then I have to go to: localhost/posts/posts/ for it to use the index() function of PostsController.
Is there any way I can just navigate to localhost/posts/ and I'll get the index() function of the PostsController running?
I know this is probably a very simple problem and I'm just missing something because I'm so tired (well that's my excuse anyway), but I've been searching around for about 3 hours now and wouldn't mind a helping hand.
Thanks for your time,
InfinitiFizz
P.S. I've just realised I can dump all the cakephp installation files/folders into the root (htdocs) and then localhost/posts/ will work but I've got loads of different test websites in their own folders in htdocs/ and so I'd rather have this posts test in its own folder too, not have all the cakephp folders mixed up with all the other websites' folders.
Just to be clear what directories we're talking about, a Cake installation comes with these folders:
/
app/
webroot/
cake
You will have to hit the top / directory with your browser to get a response from Cake at all. All routes are relative to that top / directory.
Let's say you have installed Cake in your web server like so:
/
htdocs/
someotherproject/
mycakeapp/
app/
webroot/
cake/
The htdocs directory is the root of your web server. If you go to http://localhost/, your web server will respond with the contents of /htdocs/. It's not even invoking Cake, so Cake can't route anything.
You'll have to open http://localhost/mycakeapp/ to invoke Cake. From there, Cake will do its routing. The Cake route Router::connect('/', …) corresponds to the URL http://localhost/mycakeapp/. All Cake routes are relative to the app installation path. The Cake routing is app-internal routing, it does not correspond to the absolute URL.
If you want http://localhost/ to be your Cake app, Cake will need to be the only app residing in /htdocs/. You can't have multiple apps in the root and yet have any one of them be "the root app"†‡.
For local development purposes this should be perfectly fine. When uploading the app to a real server with a real domain you'll usually make it the one and only app.
† Well you could, with elaborate rewrite-rules, virtual host configurations or by placing files in Cake's /app/webroot/ folder. Usually more hassle than it's worth though, keep your projects separate.
‡ You can't have your Cake and eat it, too. zing

Categories