Why do we use Action in Symfony2 controller methods?

I just started working on the symfony2 book. I wonder why we called our controller functions Action:

public function [something]Action() { // ...

In each example in the book so far, and all the code that I see online Actionis the name of the function. Is there a reason for this?

This works great:

<?php

// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;

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

class LuckyController extends Controller{

    /**
     * @Route("/lucky/number/{count}")
     */
    public function countTESTING($count){
        return new Response(
            '<html><body>I DONT HAVE TO CALL THIS somethingACTION</body></html>
        ');
    }

}
?>

I tried this, but I see no mention or reasoning as to why. Can someone explain why we use this suffix?

+4
source share
2 answers

This is just a convention. You can use these suffixes, but you can also do without it.

If you

public function somethingAction()

:

index:
    path:      /path_for_something
    defaults:  { _controller: AppBundle:Index:something }

_controller , . , AppBundle:Index:something :

  • : AppBundle
  • : IndexController
  • : somethingAction

. Symfony , . .

, , action , , symfony2 / , . .

, :

index:
    path:      /something
    defaults:  { _controller: AppBundle\Controller\IndexController::indexAction }

, :

, .

+12

public function somethingAction(){}

, .

,

private function something(){}

, yml , , yml ... !

-3

Source: https://habr.com/ru/post/1611477/


All Articles