简体   繁体   中英

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, eg 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM