【问题标题】:Routing issue with Symfony 3.4Symfony 3.4 的路由问题
【发布时间】:2018-02-05 11:44:11
【问题描述】:

我正在尝试通过 localhost (Xampp) 访问 Symfony 项目中文件夹中的索引文件。尽管我正在编写以下路径,但它找不到该页面:http://localhost/google_analytics_app/consolytics-app/analytics/ tree-folder-structure

按照说明,我在 viwes/analytics/ 文件夹下的 index.html.twig 文件中使用基本文件

{% extends 'base.html.twig' %}

并在文件夹/analytics/index.html.twig 下使用我自己的索引文件。 我还在 src/AppBundle/Controller/IndexController.php 中使用了注释:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Library\GoogleAnalytics;
use Doctrine\Common\Util\Debug as dump;

class IndexController extends Controller
{
    /**
     * @Route("/analytics/index", name="homepage")
     */
    public function indexAction(Request $request)
    {

        ....

  return $this->render('index.html.twig');
    }
}

输入路径时会显示 404 错误消息。有人可以帮助解决这个路由问题吗?在我的 composer.json 文件中:

"require": {
"twig/twig": "^1.0||^2.0"
},

【问题讨论】:

  • 你没有忘记在 routing.yml 文件中注册路由吗?
  • 如果你声明你的路由为/analytics/index,你应该调用相同的url(你忘了/index部分)http://localhost/google_analytics_app/consolytics-app/analytics/index
  • @zalex 但这就是注释的作用?我的意思是你不必注册到路由文件,只在控制器中..但是在尝试了 routing.yml 文件之后我仍然有同样的问题:index: resource: app/Resources/views/analytics/index.html prefix: /analytics/index@goto 我也试过了,但仍然是一样的。
  • 您必须注册您的路线。 symfony.com/doc/3.4/routing.html#loading-routes

标签: symfony xampp


【解决方案1】:

问题是你没有在 routing.yml 文件中注册你的路由。

您必须了解注释是描述路由,但是在您在 routing.yml 文件中注册该路由之前,您的路由器组件现在对您的路由没有任何内容。路由器从此文件 (v3.4) 获取您的路由。

https://symfony.com/doc/3.4/best_practices/controllers.html#routing-configuration

# app/config/routing.yml
app:
    resource: '@AppBundle/Controller/'
    type:     annotation

注意类型参数。注释告诉路由器你的路由实际上是注释:)

【讨论】:

    【解决方案2】:

    您的渲染方法参数错误。它必须返回 $this-&gt;render('views/analytics/index.html.twig');

    【讨论】:

      猜你喜欢
      • 2017-09-10
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      相关资源
      最近更新 更多