Home > Backend Development > PHP Tutorial > Symfony2 Demo配置失败

Symfony2 Demo配置失败

WBOY
Release: 2016-06-06 20:50:00
Original
915 people have browsed it

我想添加一个页面,访问链接为/hello

配置步骤如下:

1.在app/AppKernel.php文件中加入代码:

<code class="lang-php">public function registerBundles() {
$bundles[] = new Acme\HelloBundle\AcmeHelloBundle();
return $bundles;
}
</code>
Copy after login
Copy after login

2.app/config/routing.yml

<code class="lang-yml">acme_hello:
  resource: "@AcmeHelloBundle/Resources/config/routing.yml"
  prefix: /
</code>
Copy after login
Copy after login

3.src/Acme/HelloBundle/Resources/config/routing.yml

<code class="lang-yml">hello:
    pattern:  /hello/{name}
    defaults: { _controller: AcmeHelloBundle:Hello:index }
</code>
Copy after login
Copy after login

4.src/Acme/HelloBundle/Controller/HelloController.php

<code class="lang-php">namespace Acme\HelloBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class HelloController 
{
    public function indexAction()
    {
        return new Response("Hello World");
    }
}
</code>
Copy after login
Copy after login

我几乎按照文档的步骤来做了,但还是出现了一些问题:

<code>Cannot import resource "D:/xampp/htdocs/Symfony/app/config\routing.yml" from "D:/xampp/htdocs/Symfony/app/config/routing_dev.yml". (Unable to parse at line 2 (near "resource:"@AcmeHelloBundle/Resources/config/routing.yml"").) 
</code>
Copy after login
Copy after login

文档上写到执行一个命令行可以创建一个新的bundle,并在src/Acme目录下生成新的bundle目录,我的执行如下:

<code>D:\xampp\php>php d:\xampp\htdocs\Symfony\app\console --namespace=Acme/HelloBundle --format=yml
</code>
Copy after login
Copy after login

不见有任何效果,所以就自己手动创建了

回复内容:

我想添加一个页面,访问链接为/hello

配置步骤如下:

1.在app/AppKernel.php文件中加入代码:

<code class="lang-php">public function registerBundles() {
$bundles[] = new Acme\HelloBundle\AcmeHelloBundle();
return $bundles;
}
</code>
Copy after login
Copy after login

2.app/config/routing.yml

<code class="lang-yml">acme_hello:
  resource: "@AcmeHelloBundle/Resources/config/routing.yml"
  prefix: /
</code>
Copy after login
Copy after login

3.src/Acme/HelloBundle/Resources/config/routing.yml

<code class="lang-yml">hello:
    pattern:  /hello/{name}
    defaults: { _controller: AcmeHelloBundle:Hello:index }
</code>
Copy after login
Copy after login

4.src/Acme/HelloBundle/Controller/HelloController.php

<code class="lang-php">namespace Acme\HelloBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class HelloController 
{
    public function indexAction()
    {
        return new Response("Hello World");
    }
}
</code>
Copy after login
Copy after login

我几乎按照文档的步骤来做了,但还是出现了一些问题:

<code>Cannot import resource "D:/xampp/htdocs/Symfony/app/config\routing.yml" from "D:/xampp/htdocs/Symfony/app/config/routing_dev.yml". (Unable to parse at line 2 (near "resource:"@AcmeHelloBundle/Resources/config/routing.yml"").) 
</code>
Copy after login
Copy after login

文档上写到执行一个命令行可以创建一个新的bundle,并在src/Acme目录下生成新的bundle目录,我的执行如下:

<code>D:\xampp\php>php d:\xampp\htdocs\Symfony\app\console --namespace=Acme/HelloBundle --format=yml
</code>
Copy after login
Copy after login

不见有任何效果,所以就自己手动创建了

我曾经遇到过因为用tab导致yml解析错误的问题。第一个问题应该是文件格式不对。 第二个问题你光加namespace没用的。console里应该是create:bundle命令后面带namespace和format才好用。

从错误信息判断,应该是@AcmeHelloBundle/Resources/config/routing.yml这个文件里yaml语法不对,看看是不是缩进或者敲了非英文的空格之类的问题。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template