404 HTTP errors not being rendered in JSON using Zend framework 2 - zend-framework2

I'm creating a simple restful api using zend framework2 and I've references Rob Allen's notes on the subject as well as this excellent tutorial by http://hounddog.github.com/blog/getting-started-with-rest-and-zend-framework-2/
below is my module_config.php. You'll see that I have routes and the JSON view strategy configured. My understanding is that when you set up the JSON strategy in this fashion it accommodates all modules. The issue is that when an invalid route is entered the 404 response is sent back in html even though the Accept header is requesting Application/json.
I've been struggling with this for a 2 days now any advice or help would be appreciated
This curl call to the api generates the expected 404 error.
curl -i -H "Accept: application/json" http://myapi-dev.local/xxx/1
Module_config.php
return array(
'controllers' => array(
'invokables' => array(
'Dips\Controller\Roles' => 'Dips\Controller\RolesController', //maps controller alias to a physical controller
),
),
'router' => array(
'routes' => array(
'dips' => array(
'type' => 'segment',
'options' => array(
'route' => '/dips/roles/:apikey/:uname/:appname',
'constraints' => array(
'apikey' => '[a-zA-Z0-9]+',
'uname' => '[a-zA-Z]+',
'appname' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Dips/Controller/Roles',
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
);

You would need to implement an additional rendering strategy and put it in the stack above the default 404 rendering strategy. Alternatively, you could extend the existing ViewJsonStrategy to include error handling.
The default 404 Strategy is Zend/Mvc/View/Http/RouteNotFoundStrategy. If you look at it's detectNotFoundError method, you can see when it gets triggered.

Related

zend 2: Unable to render template ... resolver could not resolve to a file

I'm going through the zend 2 getting started tutorial and I hit a wall. I am at the point in the tutorial where my action controller loads a view via the indexAction():
public function indexAction() {
return new ViewModel(array(
//$albums inside index.phtml will contain data from this method
'albums' => $this->getAlbumTable()->fetchAll()
));
}
But when loading the page I see this error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
At this point I realized I don't know what the hell is happening. I don't even know where to begin troubleshooting this error. Before I scan all of the files for typos I'd really like to understand how this error can occur.
here is my modul.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' =>
'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'ablum' => __DIR__ . '/../view',
),
),
);
The error "Unable to render template "album/album/index" means that you have to add the index.phtml file under the /album/album directory under the 'Album' module's view directory. The index.phtml view template file is used for rendering the view for the index action of the AlbumController controller of the Album module. Because this file seems to be missing, the view template resolver couldn't find it.
In Zend Framework 2, you implement a view as a template file, which is a file
having .phtml extension ("phtml" stands for PHP+HTML). View templates have such
a name because they usually contain HTML code mixed with PHP code snippets used
for rendering the web pages. Views typically live inside of the view subdirectory of the module.
For beginner, I would recommend to read the Using Zend Framework 2 book. With this e-Book, you can save your time and efforts learning ZF2.
Also, the path is case sensitive i.e. it should be all be in lowercase album/index/index both folders' name and index.phtml else phpRenderer will not be able to trace the view file and render it.
You can also check if you have the following in your module.config.php
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],

How to make ZfcUser use an abstract db_adapter

I'm using Zend Framework 2 and I've installed the module ZfcUser.
Everything was working correctly until I've decided to add a second db adapter in my config/autoload/global.php
<?php
return array(
'db' => array(
'adapters' => array(
'cleardb' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=secret;host=secret.com',
'driver_options' => array(
...
),
),
'other' => array(...)
)
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstract'
=> 'Zend\Db\Adapter\AdapterAbstractServiceFactory',
),
),
);
Now, ZfcUser can't find my default db adapter.
In the zfcuser.global.php I've updated this line:
'zend_db_adapter' => 'cleardb'
This is the error I get when I try to login:
An alias "zfcuser_zend_db_adapter" was requested but no service could be found.
Your help is really appreciated.

Zend Framework 2, route with query fails only if route has child routes

Since 2.1.4 the Query route is deprecated, I'm routing to my blog like this:
'cro-blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/blog',
'defaults' => array(
'controller' => 'CroBlog\BlogController',
'action' => 'index',
),
),
),
And link to pages like /blog?p=x where x is the page number. This works perfectly until I add a child route. Linking to /blog still works but linking to pages give a 404 (more specific 'The requested URL could not be matched by routing.'). This is my current setup:
'cro-blog' => array(
'type' => 'Literal',
'options' => array(
'route' => '/blog',
'defaults' => array(
'controller' => 'CroBlog\BlogController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'post' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:slug',
'constraints' => array(
'slug' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'action' => 'post',
),
),
),
),
),
I'm using a Segment child route but the same problem exists with a Literal child route.
Any way to keep the page query and the child routes?
This is solved. If anyone experiences the same issues, this is what worked for me.
Clear all cache (EdpSuperluminal, APC etc.)
Remove Zend library
Run your application without Zend library (giving loads of errors of course, but this seemed crucial to me to make it work)
Re-install Zend library
Run your application again
I installed 2.1.5dev and it fixed the issue. Wanted to make sure whether it was a bug or not so tried again with 2.1.4 and the issue was still fixed. So this is my conclusion. Cache can be a weird thing.
Edit Jumped to (the wrong) conclusions too soon. Fired up Zend Studio 10 and now it bugs again. I think Zend Studio 10 is reverting some files (not all since Query route did got marked as deprecated). Still, consider this solved, just need to find a way to tell Zend Studio 10 to use 2.1.4. Thanks!

404 error, Zend Framework 2 The requested URL could not be matched by routing

I'm getting:
A 404 error occurred
Page not found. The requested URL could not be matched by routing.
My module.config.php file is:
'router' => array(
'router' => array(
'Test' => array(
'type' => 'Segment',
'options' => array(
//http://localhost/Test/Test
'route' => '/Test[/[:action]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Test\Controller\Test',
'action' => 'Test'
),
),
),
),
),
help please, i am new in Zend Framework 2 !
You should use configuration like Application module in ZendSkeletonApplication:
'router' => array(
'routes' => array(
'test' => array(
'type' => 'Literal',
'options' => array(
'route' => '/book',
'defaults' => array(
'__NAMESPACE__' => 'Test\Controller',
'controller' => 'Test',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
You just add the following code:
'test' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Test\Controller',
'controller' => 'Test',
'action' => 'index',
),
),
),
add this code to 'child-routes' key and after that you'll access to url: localhost/:controller_name/:action_name/:id (example : http://zf.dev/test/index or http://zf.dev/test/add/1). And now it's work!
This code can fix error 404 for tutorial in zf2 documentation.
You have a typo, try this:
'router' => array(
'routes' => array(
Routes rather than router twice..
Achieve fix it, I was missing the letter "d", was thus: Zend \ Loader \ StandarAutoloader I added the "d": Zend \ Loader \ StandardAutoloader. Greetings Friends. TIP: Zend Studio 10 and his version de ZF2 run perfect for this moment !
Please check the .htaccess file and index.php files. If these are exist in public folder means, you have to use the url as
http://localhost/public/Test/Test.
Your codes are almost right. Andrew has guided you well. Let me know your response.
1.You should check also application.config.php
and add your module name into RETURN array.
return array(
'modules' => array(
'Application',
'your_module',
.....
),
2.If Doesn't.Check route array in module.config.php
I will suggest also check the data folder that has cached config files, config files being cached in dev install also may cause this issue.
delete files inside data/cache and try.
PS: if you are just starting try with blog module on zend site it is for beginners and more updated with new versions.
https://framework.zend.com/manual/2.4/en/in-depth-guide/first-module.html

How to select controller namespace with Zend Framework 2 routing?

I have this as part of my DI config, from the skeleton applicaiton:
'routes' => array(
'default' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'index',
),
),
),
I would like to make the following routings:
http://me.com/mycontroller/myaction -->
controller=Applicaiton\Controller\Mycontroller
method=myactionAction
However, the above config produces:
http://me.com/mycontroller/myaction -->
controller=Mycontroller
method=myactionAction
As you can see, the namespace for the controller is missing. Where/how do I put the namespace in? (I know I could make a DI alias for every controller, but that would deafeat the purpose of having segment matching.)
It is perfectly possible to achieve what you were originally trying to achieve. In your defaults, you could have added a __NAMESPACE__ key and slightly altered the controller key.
You could have done this:
'default' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
),
The only other change that would be needed is when you register your controller as an invokable in your module configuration you would have to do it like so:
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
),
),
Take note to how I used Index in the key instead of IndexController.
This __NAMESPACE__ key of the defaults definition of a route and the removing of "Controller" at the end of the key in the invokables array behavior isn't mentioned anywhere in the documentation that I could find. I actually gleaned this information from how the routing of the ZendSkeletionApplication works:
https://github.com/zendframework/ZendSkeletonApplication/blob/2ce1cf0dd40046024970d87d3998e16cde41c7db/module/Application/config/module.config.php
You should not use segments for controllers in your routes. Segment matching is possible for actions and other parameters, but not for controllers. You might use aliases to help mycontroller match MyNamespace\Mycontroller, but it is not recommended. The best, easiest and with the most performance way is to have routes for every controller, use segments for actions if necessary.

Resources