mf1/trailing-slash-bundle

Mf1TrailingSlashBundle

dev-master 2017-11-10 21:02 UTC

This package is not auto-updated.

Last update: 2024-09-29 03:52:40 UTC


README

此包使您在Symfony路由中的尾部斜杠方面获得一致性。

这个想法最初是在创建API并需要处理Symfony路由要求时产生的,即至少需要一个/而不是空路径。Symfony非常有帮助地处理了具有尾部斜杠的路由之间的重定向,到没有尾部斜杠的(或者反过来,我忘记了是哪一个)。由于API路由通常暴露给用户,我认为应该既有统一性(要么全部有或没有尾部斜杠),又尽可能减少重定向。

例如

# app/config/routing.yml
routes:
    prefix: /api/
    resource: @AppBundle/Resources/config/routing.yml

# src/AppBundle/Resources/config/routing/yml
routes_1:
    path: /
    ...

routes_2:
    path: /nested
    ...

将变成..

$ bin/console debug:router

 ---------- -------- -------- ------ -----------------------------------
  Name       Method   Scheme   Host   Path
 ---------- -------- -------- ------ -----------------------------------
  routes_1   ANY      ANY      ANY    /api/
  routes_2   ANY      ANY      ANY    /api/nested

在理想的世界里,它们将是 /api/api/nested(或者 /api//api/nested/... 由您选择)。

所以..

安装

  1. 使用composer下载Mf1TrailingSlashBundle
  2. 启用包

步骤1. 使用composer下载Mf1TrailingSlashBundle

使用命令行要求包

$ composer require mf1/trailing-slash-bundle

步骤2. 启用包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Mf1\TrailingSlashBundle\Mf1TrailingSlashBundle(),
        // ...
    );
}

配置

配置非常简单。基本上,您只需设置一个路径,并说明如何处理尾部斜杠。

注意嵌套路由将遵循与其最接近的父路由相同的规则,直到它们达到一个新的匹配规则。

mf1_trailing_slash:
    rules:
        # /remove-slash/ will become /remove-slash
        # /remove-slash/a/ will become /remove-slash/a
        # /remove-slash/b will become /remove-slash/b
        - { path: /remove-slash/, slash: remove }
        # /add-slash will become /add-slash/
        # /add-slash/a will become /add-slash/a/
        # /add-slash/b/ will become /add-slash/b/
        - { path: /add-slash, slash: add }
        # /leave-as-default will stay as /leave-as-default
        - { path: /leave-as-default, slash: no_change }
        # /nested/ will stay as /nested/
        - { path: /nested/, slash: no_change }
        # /nested/add will become /nested/add/
        # even though /nested/ is set to not change
        - { path: /nested/add, slash: add }
        # /nested/remove/ will become /nested/remove
        # even though /nested/ is set to not change
        - { path: /nested/remove/, slash: remove }

许可证

此包使用MIT许可证。请参阅包中的完整许可证。

报告问题或功能请求

问题和功能请求在Github问题跟踪器中进行跟踪。