首页 文章

Symfony 3.3.6添加控制器/视图获取404 - Cloud9.io

提问于
浏览
1

我在C9.IO上安装并设置了 Symfony 3.3.6 .

当我运行应用程序时,它会加载并将我带到默认页面 .
enter image description here

但是,我在添加自定义控制器和视图时遇到问题 . 我've followed all of the instructions, but I get a 404 error when trying to access the page/route. This is the url I'm访问此页面: https://symfony4-XXXX.c9users.io/test

我在 src/Controller/TestController.php 目录中添加了一个名为 TestController.php 的控制器 .

在这个控制器中,我有以下代码:

namespace AppBundle\Controller;

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

class TestController extends Controller
{
    /**
     * @Route("/", name="test")
     */
    public function testAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('test/test.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
        ]);
    }
}

我已将视图'test.html.twig'添加到目录'app / Resources / views / test / test.html.twig'

该文件中的代码是:

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

{% block body %}
    <p>This is my test page.</p>
{% endblock %}

我的routing.yml文件设置为:

app:
    resource: '@AppBundle/Controller/'
    type: annotation

为什么我无法访问此页面?我一直收到 404 错误 .

日志仅显示:

[2017-08-10 13:00:13] request.ERROR:未捕获PHP异常Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException:“在/ home / ubuntu / workspace / var中找不到”GET / test“的路由/cache/prod/classes.php第4173行{“exception”:“[object](Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException(code:0):在/ home /找不到\”GET / test \“的路由ubuntu / workspace / var / cache / prod / classes.php:4173,Symfony \ Component \ Routing \ Exception \ ResourceNotFoundException(code:0):at /home/ubuntu/workspace/var/cache/prod/appProdProjectContainerUrlMatcher.php:47 )“} []

1 回答

  • 2

    更改注释

    /**
     * @Route("/", name="test")
     */
    

    这样

    /**
     * @Route("/test/", name="test")
     */
    

相关问题