呈现新模板symfony 4时出现FileLoaderLoadException

时间:2018-01-03 10:21:51

标签: php symfony

我是新手,并试图增强我对这个框架的了解。在渲染模板时遇到困难,它给了我一个错误:

  

FileLoaderLoadException错误64]仅允许XML声明   文件的开头(在n / a - 第2行,第6栏)in   C:\ xampp \ htdocs \ symfony \ config \ routes / routes.xml(已加载   资源" C:\ xampp \ htdocs \ symfony \ config \ routes / routes.xml")

控制器

<?php

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class UserController extends Controller
{
    /**
     * @Route("/homepage", name="user")
     */
    public function index()
    {
        return $this->redirectToRoute('homepage');
    }
}

YAML

user:
    path:     /homepage
    controller: App\Controller\UserController::index

XML

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/routing
        http://symfony.com/schema/routing/routing-1.0.xsd">

    <route id="user" path="/homepage">
        <default key="_controller">App\Controller\Userontroller::index</default>
    </route>
</routes>

问题:我该如何解决这个问题?我错过了导致此错误的内容吗?

1 个答案:

答案 0 :(得分:1)

也许您不了解Symfony中的路由是如何工作的,对于新手来说这是正常的。

路由非常简单,你必须定义它(在yaml或XML或PHP或注释中)而不仅仅是其中之一: - )。

最佳做法建议使用注释,以便评论您的yaml并删除XML文件并按照documentation

进行操作
/**
 * @Route("/homepage", name="user")
 */

/ homepage表示此网址与此功能匹配  命名=&#34;用户&#34;它是像这样的树枝中的路径名

<a href="{{ path('user') }}">User page</a>
相关问题