1

I am using ZF2 modules, but I am not using full ZF2 framework stack.

I need to be able to pull GET parameters from URL string into my Controller.

I've created a Controller that extends AbstractActionController of ZF2. After reading ZF2: Get url parameters in controller, I have used print $this->params('line'); to try to print $_GET['line'] value, but nothing shows up.

In short, this is supposed to work, but doesn't:

//ZF2 equivalents of print $_GET['line'] (either one should work)
print $this->params('line'); 
print $this->getEvent()->getRouteMatch()->getParam('line');

I imagine it is due to lack of mechanism to populate params from $_GET. How can I get that part of ZF2 mechanism into my code?

And is it worth the trouble? For example, if I need to introduce routes, events, and pretty much bring the entire ZF2 framework into my code, and significantly rewire my app for this to work, I might want to just stick to $_GET method, at least for now.

Community
  • 1
  • 1
Dennis
  • 7,907
  • 11
  • 65
  • 115
  • Zend Framework is always praised because of it's modularity. However, practically people like to use the full stack. Which is fine, and I think it is one of the best. But you are one the few examples that should prove that the modularity works up to the core of the framework. I'm curious what the answer to this question is going to be. – user228395 Oct 16 '15 at 20:33

2 Answers2

2

I'm not sure if this is much of an answer, but it might be at least helpful. So, disclaimer: I didn't try this.

So you extended the AbstractActionController, which is nice because it's full of features. The AbstractActionController implements a whole list of interfaces that makes the controller listen to MvcEvents, uses the Service Locator, etc. If you are not supplying the controller with a ServiceLocator etc., then there is no point in extending the AbstractActionController. Use the DispatchableInterface instead! Also, read up on: http://framework.zend.com/manual/current/en/modules/zend.mvc.controllers.html

Now, if you do want to use the features of the AbstractActionController you will need the \Zend\Mvc package. Which comes with all of the features or overhead - which of these is the case depends on your application. Note that the Zend MVC application is actually very performant, but if you are not planning on reusing those components you might be actually better off without them. The manual actually indicates how to bootstrap Zend MVC: http://framework.zend.com/manual/current/en/modules/zend.mvc.intro.html#bootstrapping-an-application. If you succeed in bootstrapping, you should be fine, and be able to use your controllers.

user228395
  • 1,156
  • 1
  • 11
  • 25
0

You might try

print $this->params()->fromQuery('line');

According to this http://zf2cheatsheet.com/

miketineo
  • 34
  • 3