最近在试着用nginx+symfony搭建公司的网站,由于nginx不支持pathinfo模式,所以必须修改nginx(我使用的是nginx1.5.1)的配置文件/etc/nginx/config.d/default.conf以使其在逻辑上支持url逻辑路径到物理路径的映射,以下为我的default.conf文件内容:
1 server { 2 listen 80; 3 server_name localhost; 4 location / { 5 root /usr/share/nginx/html/symfony/web; 6 index index.html index.htm index.php; 7 } 8 location ~ ^(.+.php)(.*)$ { 9 root /usr/share/nginx/html/symfony/web; 10 fastcgi_index index.php; 11 fastcgi_split_path_info ^(.+.php)(.*)$; 12 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 13 fastcgi_param PATH_INFO $fastcgi_path_info; 14 fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; 15 fastcgi_pass 127.0.0.1:9000; 16 include fastcgi_params; 17 }
18}
实现url的逻辑映射后,就可以在网页中看到实际效果了(注意图中被圈部分):
但是,如果在prod模式下(即访问192.168.10.133/app.php/下的路径)时,提示出错“no route for get / ",这并不是我们的pathinfo配置出了问题(刚开始从这个方面着手,费老劲了),而是因为symfony2并没有为我们提供prod模式下的内容,我们需要修改symfony/app/routing.yml文件自己给他提供一个逻辑路径映射,如下为我从routing_dev.xml文件中拷过来的一个配置项:
1 # AcmeDemoBundle routes (to be removed) 2 _acme_demo: 3 resource: "@AcmeDemoBundle/Resources/config/routing.yml"
这样symfony就会在prod模式下就可以通过url逻辑映射得到src/Acme/DemoBundle/Resources/config/routing.yml文件(其内容如下所示),通过这个文件就可以找到@AcmeDemoBundle/Controller/DemoController.php文件从而显示出如上图所显示的效果了
1 _demo: 2 resource: "@AcmeDemoBundle/Controller/DemoController.php" 3 type: annotation 4 prefix: /demo
但是,实际访问http://192.168.10.133/app.php/demo/hello/bean时,并没有显示出期望的效果,这时候又报了一个错误:
Cannot import resource "/usr/share/nginx/html/symfony/src/Acme/DemoBundle/Resources/config/config.yml" from "/usr/share/nginx/html/s
ymfony/app/config/config.yml".
网上搜了下,stackoverflow上给的答案大部分是让你在resource字段前面要缩进四个字符之类的,对我的问题不适用,后来发现在上面出错信息的后面还有一行字说要在AppKernel.php中注册AcmeDemoBundle之类的,于是找到/usr/share/nginx/html/symfony/app/AppKernel.php,发现有如下一句话:
1 if (in_array($this->getEnvironment(), array('dev','test'))) { 2 $bundles[] = new AcmeDemoBundleAcmeDemoBundle(); 3 $bundles[] = new SymfonyBundleWebProfilerBundleWebProfilerBundle(); 4 $bundles[] = new SensioBundleDistributionBundleSensioDistributionBundle(); 6 $bundles[] = new SensioBundleGeneratorBundleSensioGeneratorBundle(); 7 }
至此,大功告成,我们只需要修改
array('dev','test')为array('dev','test','prod')即可,这样symfony就会在prod模式下也注册AcmeDemoBundle了,实验结果显示如下:
本文系原创,转载请注明出处:http://www.cnblogs.com/beanmoon/p/3181733.html