S
S
Sergey2016-02-26 13:45:23
symfony
Sergey, 2016-02-26 13:45:23

How to call a non-action method when declaring a controller as a service?

How to call a non-action method when declaring a controller as a service?
There is a controller class designated as a service

namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
//...

class MyServiceController extends Controller {
    /**
     *
     * @Route("/my/action", name="my_action")
     * @Method("POST")
     */
    public function myAction(Request $request)
    {
     // ...
    }

    protected function myMethod(Request $request)
    {
     // ...
    }
}

there is another controller class that overloads the myMethod method of the parent service class
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
//...
class MyOtherServiceController extends MyServiceController {
    /**
     *
     * @Route("/my/other/action", name="my_other_action")
     * @Method("POST")
     */
    public function myAction(Request $request)
    {
        // ... 
    }

    protected function myMethod(Request $request)
    {
     // ...
    }
}

and there is the controller itself, which calls the service
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
//...
class MyController extends Controller {
    /**
     * @Route("/my/controller", name="my_controller")
     * @Method("POST")
     */
    public function serviceAction(Request $request)
    {
         // Срабатывает успешно. 
         $this->forward('app.service.myAction', array(
                'request' => $request,
            ));

         // не срабатывает 
         $this->forward('app.service.myMethod', array(
                'request' => $request,
            ));
    }
}

services.yml
app.service:
        class: AppBundle\Controller\MyOtherServiceController
        arguments: []

Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrei, 2016-02-26
@VladimirAndreev

protected if changed to public - will it work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question