简体   繁体   中英

Symfony2: Route without slash in the end?

I have this controller:

/**
 * @Route("/admin/products")
 */
class ProductAdminController extends Controller
{
    /**
     * @Route("/", name="product_list")
     */
    public function listAction()
    {
    }

    /**
     * @Route("/new", name="product_new")
     */
    public function newAction()
    {

    }

    /**
     * @Route("/{id}/delete", name="product_delete")
     */
    public function deleteAction()
    {
    }
}

When I go to the page /admin/products Symfony redirects me to the /admin/products/ . But I want my routes be consistent without slash in the end.

If I omit slash in listAction:

/**
 * @Route(name="product_list")
 */
public function listAction()
{
}

Now route works OK without redirect. But I don't know it's correct or not. Because all examples use "/" for root route. Thanks in advance!

You can remove the @route annotation above the class declaration,
and then state the full route for your actions.

Beside the indexAction , other routes shouldn't display a / at the end of they URL as they're, but you will have to precede them with /admin/products as well.

class ProductAdminController extends Controller {
    /**
     * @Route("/admin/products", name="product_list")
     */
    public function listAction() {
    }

    /**
     * @Route("/admin/products/new", name="product_new")
     */
    public function newAction() {
    }

    /**
     * @Route("/admin/products/{id}/delete", name="product_delete")
     */
    public function deleteAction() {
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM