Php 在Symfony中设置正确的路由以获得正确的url和路由
我使用Symfony 2.5(最新版本)创建web应用程序。我在windows 7上使用WAMP本地服务器 当我启动应用程序时,这是我得到的Php 在Symfony中设置正确的路由以获得正确的url和路由,php,.htaccess,symfony,url,routing,Php,.htaccess,Symfony,Url,Routing,我使用Symfony 2.5(最新版本)创建web应用程序。我在windows 7上使用WAMP本地服务器 当我启动应用程序时,这是我得到的http://localhost/Symfony/web/。事实上,我想添加/主页 我有第一个名为WelcomeBundle的捆绑包和他的控制器:它涉及我的应用程序主页:Gir/WelcomeBundle/controller/HomePageController.php <?php namespace Gir\WelcomeBundle\Contr
http://localhost/Symfony/web/
。事实上,我想添加/主页
我有第一个名为WelcomeBundle的捆绑包和他的控制器:它涉及我的应用程序主页:Gir/WelcomeBundle/controller/HomePageController.php
<?php
namespace Gir\WelcomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomePageController extends Controller
{
public function indexAction()
{
return $this->render('GirWelcomeBundle:HomePage:index.html.twig');
}
}
<?php
namespace Gir\AdministrationBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AdministrationController extends Controller
{
public function indexAction()
{
return $this->render('GirAdministrationBundle:Admin:index.html.twig');
}
}
这是常规路由文件:app/config/routing.yml
GirWelcomeBundle_HomePage:
pattern: /
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
GirWelcomeBundle:
resource: "@GirWelcomeBundle/Resources/config/routing.yml"
prefix: /
gir_administration:
resource: "@GirAdministrationBundle/Resources/config/routing.yml"
prefix: /
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
gir_user:
resource: "@GirUserBundle/Resources/config/routing.yml"
prefix: /
GirWelcomeBundle_HomePage:
pattern: /homepage
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
gir_administration_homepage:
pattern: /administration
defaults: { _controller: GirAdministrationBundle:Administration:index }
requirements:
methods: GET
schemes: https
因此,当我首先到达应用程序的时,我有我的主页,因此它工作得非常完美。这是启动应用程序时的url:http://localhost/Symfony/web/
但是如果我想像这样添加路径(我在路径行中添加/homepage)Gir/WelcomeBundle/Ressource/config/routing.yml
GirWelcomeBundle_HomePage:
pattern: /
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
GirWelcomeBundle:
resource: "@GirWelcomeBundle/Resources/config/routing.yml"
prefix: /
gir_administration:
resource: "@GirAdministrationBundle/Resources/config/routing.yml"
prefix: /
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
gir_user:
resource: "@GirUserBundle/Resources/config/routing.yml"
prefix: /
GirWelcomeBundle_HomePage:
pattern: /homepage
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
gir_administration_homepage:
pattern: /administration
defaults: { _controller: GirAdministrationBundle:Administration:index }
requirements:
methods: GET
schemes: https
应用程序找不到路由,我有以下错误:
No route found for "GET /" (from "http://localhost/Symfony/")
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »
有人知道为什么吗
然后,当我试图启动管理页面时,浏览器会显示该页面是一个无法访问的网页
我真的不明白。
该捆绑包名为AdministrationBundle,这是controller代码:Gir/AdministrationBundle/controller/AdministrationController.php
<?php
namespace Gir\WelcomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomePageController extends Controller
{
public function indexAction()
{
return $this->render('GirWelcomeBundle:HomePage:index.html.twig');
}
}
<?php
namespace Gir\AdministrationBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AdministrationController extends Controller
{
public function indexAction()
{
return $this->render('GirAdministrationBundle:Admin:index.html.twig');
}
}
有人能帮我解释一下原因吗
更新帖子:
这是web文件夹中的.htacess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/app.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the start page because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
DirectoryIndex app.php
重新启动发动机
#自动确定RewriteBase并将其设置为环境变量。
#如果您使用Apache别名进行大规模虚拟托管或安装
#在子目录中的项目中,将在基本路径前面加上前缀,以允许正确的
#解析app.php文件并重定向到正确的URI。会的
#也可以在没有路径前缀的环境中工作,提供一个安全的单一大小
#适用于所有解决方案。但是,在这种情况下,您不需要它,您可以进行评论
#以下两条线路可消除开销。
RewriteCond%{REQUEST_URI}:$1^(/.+)/(.*):\2$
重写规则^(.*)-[E=基:%1]
#设置apache删除的HTTP_授权头
RewriteCond%{HTTP:Authorization}。
重写规则。*-[E=HTTP\U授权:%{HTTP:AUTHORIZATION}]
重写cond%{REQUEST_FILENAME}-F
重写规则^(.*)$app_dev.php[QSA,L]
#重定向到不带前端控制器的URI以防止重复内容
#(带或不带“/app.php”)。仅在初始路径上执行此重定向
#由Apache重写,而不是在后续周期中重写。否则我们会被解雇
#无休止的重定向循环(请求->重写到前端控制器->
#重定向->请求->…)。
#所以,如果你得到一个“太多重定向”错误或你总是得到重定向
#因为您的Apache没有公开重定向的状态,所以无法访问起始页
#环境变量,您有两种选择:
#-通过注释以下两行或两行禁用此功能
#-使用Apache>=2.3.9,用结束标志替换所有L标志,并删除
#第二步(最佳解决方案)
RewriteCond%{ENV:REDIRECT_STATUS}^$
重写规则^app\.php(/(.*)|$)%{ENV:BASE}/$2[R=301,L]
#如果请求的文件名存在,只需提供它即可。
#我们只想让Apache服务于文件,而不是目录。
RewriteCond%{REQUEST_FILENAME}-f
重写规则[L]
#将所有其他查询重写到前端控制器。
重写规则.?%{ENV:BASE}/app.php[L]
#当mod_rewrite不可用时,我们指示临时重定向
#将起始页显式地发送到前端控制器,以便网站
#生成的链接仍然可以使用。
重定向匹配302^/$/app.php/
#不能改为使用RedirectTemp
根据您的描述,我猜您的路由.yml
看起来像这样:
GirWelcomeBundle_HomePage:
pattern: /
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
GirWelcomeBundle_HomePage:
pattern: /homepage
defaults: { _controller: GirWelcomeBundle:HomePage:index }
requirements:
methods: GET
schemes: https
这种方法的问题在于,您的GirWelcomeBundle\u主页
。解决方案很简单:
名称路由不同,例如:GirWelcomeBundle\u Root
和GirWelcomeBundle\u Home
另一个可行的解决方案是只在一条路线中定义它:
GirWelcomeBundle_HomePage:
pattern: /{homepage}
defaults: { _controller: GirWelcomeBundle:HomePage:index, homepage: 'homepage' }
requirements:
methods: GET
schemes: https
将路由更改回/homepage,检查
php应用程序/控制台路由器:debug
-output,您将看到没有/只有一个用于/homepage的路由
因此,您需要另一个控制器/路由对从/重定向到/主页。您是否正确设置了虚拟主机?例如DocumentRoot/var/www/gir.dev/web也使用正确的.htaccess启用了mod_rewrite?我已经更新了我的帖子,所以你可以看到我的htaccess代码。你的虚拟主机设置如何?@Rooneyl,我还没有创建虚拟主机,它到底是如何工作的?这是同样的问题。我按照您的建议更改了代码,但仍然不起作用。@Julien您在开发环境中吗?如果没有,请清除缓存
php应用程序/控制台ca:c--env=prod
我已经清除了缓存,没有任何更改。我确信我在开发环境中。