Instantiate a \Zend\Stdlib\RequestInterface - php

I want to use the method match(\Zend\Stdlib\RequestInterface) of Zend\Mvc\Router\Http\TreeRouteStack.
But I don't find any way to get the request I'm searching for.
This articles says how to get a request object outside a controler. I'm searching for an equivalent with ZF2

Finally I found how to fix by browsing the unit testing of the zf2
https://github.com/zendframework/zf2/blob/master/tests/ZendTest/Mvc/Router/Http/TreeRouteStackTest.php
The Zend\Http\PhpEnvironment\Request; will give a valid Request for the match method
use Zend\Http\PhpEnvironment\Request as PhpRequest;
/*TreeRouteStack*/
$request = new PhpRequest();
$stack->match($request);

Related

Difference between match parameter and RequestContext in Symfony Router UrlMatcher

I don't understand the semantics of the Symfony Routing Component's API.
From the first code example on the Routing Component documentation page:
$context = new RequestContext('/');
$matcher = new UrlMatcher($routes, $context);
$parameters = $matcher->match('/foo');
Why is the hostname and HTTP method passed in via $context and the path via a parameter to match()? Or is it? There is also a path parameter in the RequestContext constructor.
One gets the impression match() is supposed to be called multiple times with different paths within one request, which I can't imagine would ever happen.
After integrating the Routing Component into my application, I now have a hunch why
it was done like that.
Most of the properties of the RequestContext - method, request body, get parameters - can be used without modification, but depending on the desired path structure and server configuration (rewrite rules, etc.) there are multiple ways in which the path needs to be preprocessed.
This doesn't explain why the path is passed to the match() function and the request object is passed to the constructor, but it does explain why they are passed in separately.

ZF3: Read out url parameter in PhpRenderer

I want to upgrade from ZF2 to ZF3 right now and has the following problem with using URL parameter in my PhpRenderer.
In ZF2 I use the HelperPluginManager to get Application, then the MvcEvent and finally the routeMatch:
$routeMatch = $this
->getHelperPluginManager()
->getServiceLocator()
->get('Application')
->getMvcEvent()
->getRouteMatch();
$parameterAction = $routeMatch->getParam('action');
In ZF3 there is a deprecation warning with using the getServiceLocator() (which makes sense, because it only returns the creationContext from the ServiceManager). I want to find a way not trigger the warning.
Configure the Application as a factory-using class (using \Zend\Mvc\Service\ApplicationFactory::class) also not works, because:
Zend\View\HelperPluginManager can only create instances of Zend\View\Helper\HelperInterface and/or callables.
Is there any way to get the Application context in my template (or better even the parameters of the URL)?
Your question title is "ZF3: Read out url parameter in PhpRenderer" while inside your question you asked another one.
You can get this in any controller (get URL parameters in ZF3):
$URLparam = (string)$this->params()->fromQuery('parameter', '');
For routeMatch, if you have a MvcEvent just use;
$event->getRouteMatch()
If you have container;
$container->getApplication()->getMvcEvent()->getRouteMatch();
If you want to access routeMatch in view there's no way except view helper like tasmaniski/zend-current-route

Sub request in Laravel 5

I'm looking to do sub requests in my API, to other parts of my API. I have done this before in Symfony - but I'm not sure how to achieve this in Laravel.
$url = route('some.route', ['param' => $val]);
$request = Request::create($url, 'get', []);
Route::dispatch($request);
Always seems to fail giving something along the lines of
Class api does not exist
So I've tried
app()->handle($request);
This works, but processes a request, but I cant handle any exceptions thrown (e.g. validation as the app layer handles it and throws a html response)
Handle has a signature of the HttpKernelInterface, so can take a property of sub requests and catch exceptions - but these are not used....
...->handle($request, HttpKernelInterface::SUB_REQUEST, false);
Is it possible to do this in Laravel without having to send an actual http request?
Thanks
So after digging deep into the framework, it seems that this way is not possible as it stores some values as statics and they're not updated via the dipatch() method.
So ive created this package to handle setting and reapplying the original values.
https://github.com/myerscode/laravel-sub-request

Get requested service in Restler iAuthenticate implementation

I'm using Restler to develop a REST api and I need to get the requested service from the iAuthenticate implementation.
So far I have managed to get here:
$m = preg_match('/.+?\/(?P<api>.+?)\/(?P<service>\w+)/', $_SERVER['REQUEST_URI']);
and $_SERVER['REQUEST_URI'] has this form: /somedir/apiclass/requestedservice?...
I've tried my regex here: http://www.spaweditor.com/scripts/regex/index.php with my actual uri
and it works perfectly. When I try to parse the request url inside my iAuthenticate implementation it just don't work.
Does anybody know how to enable regex within restler iAuthenticate implementation? How can I display errors in Restler instead of a blank page?
Thanks!
[EDIT]
I wasn't passing the variable to store the match object, preg_match recieves a third argument to store it and just returns a boolean. Case closed.
I finally solved it like this:
preg_match('/.+?\/(?P<api>.+?)\/(?P<service>\w+)/', $_SERVER['REQUEST_URI'], $mo);
$api = $mo['api'];
$service = $mo['service'];

Read HTTP headers in Controller (Zend Framework)

Long story short:
I'm building a skeleton application for Zend Framework and I got to the part where I need to setup the api module. I'm using Zend_Rest_Controller for this job. All is ok up to this part where I need to get the HTTP headers in a controller to verify the api key.
On various tutorials I've read on the web the thing is done via a front controller plugin, but I need it to be more "plug and play" than that (checking each time the config of the application, deciding which module is the api and so on).
I tried what seemed most obvious $this->getRequest()->getHeaders() but doesn't seem to work, at least not for the HTTP headers where I'll be seding my api key. Neither the reponse object.
Can anyone help me with this one?
I found a way of doing this after all :)
On the preDispatch() method in your controller you can do the following:
public function preDispatch()
{
$request = new Zend_Controller_Request_Http();
$key = $request->getHeader('x-apikey');
}
It seems that Zend_Controller_Request_Http object gives you acces to the headers. More info on the Zend_Controller_Request_Http you can find here
As Bogdan said, you can find that information in the Zend_Controller_Request_HTTP class. It can be found in the controller itself by doing :
$this -> getFrontController() -> getRequest() -> getHeader('Content-Type');
Unfortunatly, you can't access all headers at once but what ZF does is just use apache_request_headers() function if available on the server to get them.

Categories