0

I started to explore the world of Symfony 2 now and face with some realy strange problems i would not think they can occure in such a professional framework. I will show you the problems i face one by one:

1) How to get the recent actionName? I found only this solution which is imho semiprofessional:

 $request->attributes->get('_controller');
 // will get yourBundle\Controller\yourController::CreateAction

 $params = explode('::',$request->attributes->get('_controller'));
 // $params[1] = 'createAction';

 $actionName = substr($params[1],0,-6);

Is this serious, i have to do some extra-work to get it, why.. Is there a better solution? Creating a base controller class with a method e.g. getActionName(), but why do i have to implement such basic functionality in a framework. Is there a other way?

2) When i forward a request the code in 1) will not work.

 $request = $this->container->get('request');
 $getParameterList = $request->query->all();
 if (!empty($getParameterList['mode'])
     && $getParameterList['mode'] == 1) {
   return $this->forward('AcmeDemoBundle:Routing:lawyersearch', array(), $getParameterList);
 }

The reason why it will not work is that "AcmeDemoBundle:Routing:lawyersearch" is a other format than when i came directly from a route. Second problem here is that i have to forward the GET-paramters as well(i think POST too). Is there a way that i do not have to care about it?

3) How to use a default template without using this annotation:

/**
 * @Template()
 */
public function indexAction()
{
  return array();
}

I do not want to have above all my methods this annotation; i know i can put it on the top of the class definition. Is there a way to achieve this? The only solution i see, is to write a BaseController that determines by a method out of the module/controller/action the default template.

4) I found classes that use public attributes e.g. Symfony\Component\Validator\Constraints\Length with e.g. public $max;

How to solve this? Very strange because this is not professional to use public attributes.

I hope someone has easy solutions for this. It would be realy dissapointing if Symfony 2 has so much strange behaviour in so much cases. 4 strange things i 2 days since i began to explore it. It gives me the feeling that there is much more when i continue.

Please confirm that there are no other solution by the framework or which is the solution. Thank you

Gizzmo
  • 691
  • 8
  • 21
  • Symfony2 is just a framework, and it should not provide ALL and EVERY functionality you need. Just make that functionality yourself, if you can't find it. – xurshid29 Nov 27 '14 at 16:59

2 Answers2

1

1) Use Constant: __FUNCTION__ http://php.net/manual/en/language.constants.predefined.php

2) Try setMethod on $request: $this->get('request')->setMethod('POST');

3) I do not know, probably not possible.

4) Symfony\Component\Validator\Constraints\Length is one of constraints: http://symfony.com/doc/current/book/validation.html#constraints

1

1) By accessing the '_controller' parameter of the request, you are delving into the internals of Symfony2. They rarely document anything related to this outside of routing. You should use controller actions more definitively, don't try to automate too much on this level.

2) Symfony2 can't account for highly dynamic controllers. You know it is possible to call ->forward more than once, and within the same controller action. This creates a nesting nightmare that the Symfony developers weren't prepared to deal with.

This is one of the reasons $request = $this->container->get('request'); is now deprecated in favour of $stack = $this->container->get('request_stack');. Because forwarding needs to create new internal requests.

3) Also deprecated. Symfony2 best practices now discourages the use of @Template() with empty parameters because of the potentially volatile development of actions/templates. You are supposed to explicitly define which template to use, if you use one at all. This comes in handy when dealing with data-only responses. You wouldn't want your responses to use a template automatically as this would result in unexpected behaviour in your design.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • Thx Flosculus, 1) sad that is is like that, i have to support old URLs and need such funtion, 2) than this do not work eihter => i have to to do it on myown 3) is ok 4) is simply bad programming than... – Gizzmo Nov 28 '14 at 07:29
  • @Gizzmo While 4) may be a little inconsistent with the general design of symfony, it is not bad practice. The use of public properties is a paradigm of older OOP languages and has been incorporated into PHP. If ever true getters and setters are implemented to PHP, you would no longer consider it bad design. – Flosculus Dec 01 '14 at 09:05
  • wut;-) it is not bad practice? sure it is bad ihmo. But What you mean by "true getters and setters..." – Gizzmo Dec 01 '14 at 12:09
  • @Gizzmo http://stackoverflow.com/a/17881367/1692906 These are ideal. Opposed to the current `get*` and `set*` methods we are used to creating. – Flosculus Dec 01 '14 at 15:54