【问题标题】:Take out the "public" part from URL from Symfony从 Symfony 的 URL 中取出“公共”部分
【发布时间】:2020-09-12 00:47:57
【问题描述】:

我正在使用 symfony 框架进行培训,但遇到了障碍。我有这个前端控制器:

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class FrontController extends AbstractController
{
    /**
     * @Route("/", name="main_page")
     */
    public function index()
    {
        return $this->render('front/index.html.twig');
    }
    
    /**
     * @Route("/public/video-list", name="video_list")
     */
    public function videoList()
    {
        return $this->render('front/video_list.html.twig');
    }
    
    /**
     * @Route("/video-details", name="video_details")
     */
    public function videoDetails()
    {
        return $this->render('front/video_details.html.twig');
    }
    
}

并且在 index.html.twig 我在一些链接中有以下内容:

 <nav class="my-2 my-md-0 mr-md-3">
   <a class="p-2 text-dark" href="{{ path('video_list') }}">Funny</a>
   <a class="p-2 text-dark" href="{{ path('video_list') }}">Scary</a>
   <a class="p-2 text-dark" href="{{ path('video_list') }}">Unbelievable</a>
   <a class="p-2 text-dark" href="{{ path('video_list') }}">Inspirational</a>
   <a class="p-2 text-dark" href="{{ path('video_list') }}">Motivating</a>
   <a class="p-2 text-dark" href="admin/my_profile.php">My account</a>
</nav>

但是当我点击这些链接时,我得到object not found。当我在 URL 中尝试带有 public 的 URL 时,例如 /public/video-list/,它起作用了。我希望应用程序忽略 URL 上的这个 public 部分。我尝试将路由更改为 public/video-list,但也没有成功。

我在 Windows 8.1 上使用 Xampp,使用 Symfony 框架版本 5.1.2。我用 composer 安装了 apache-pack。如何调整这个项目以忽略这个 public 部分?

【问题讨论】:

标签: php symfony twig


【解决方案1】:

您的路线“video_list”是到FrontController::videoList 在注释中声明:

    /**
     * @Route("/public/video-list", name="video_list")
     */
    public function videoList()
    {
        return $this->render('front/video_list.html.twig');
    }

您已将类方法 FrontController::videoList 注释为名为“video_list”的路由,并具有路径“/public/video-list”。要从路由路径中删除“/public”部分,只需相应地修改@Route 参数:

    /**
     * @Route("/video-list", name="video_list")
     */
    public function videoList()
    {
        return $this->render('front/video_list.html.twig');
    }

然后您应该使用"cache:clear" 命令刷新您的注释缓存。那么生成的路径应该会改变。

您可以使用"debug:router" 命令查看结果是否与更改匹配。

【讨论】:

  • 我们会看看原始海报是否会回来,但我怀疑他们在故障排除过程中有公众参与。但也许不是。
  • 如果我从路由注释中取出 public 部分,它不起作用,因为浏览器显示它找不到对象。当我放置 public 部分时,它可以工作。有效:@Route("/public/video_list", [...] 无效:@Route("/video_list", [...] 即使在清除缓存的命令之后
猜你喜欢
  • 2023-01-17
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多