I was trying to add new error 404 page for my module. I have Application and my own Admin module. For Application I use default 404.phtml, for my new module I created admin404.phtml but I have no idea how to run it. There are a lot of options how to change layout for modules but I couldn`t find answer for my question.
Can anyone help me?
When a page could not be found or some other error happens inside of your web application,
a standard error page is displayed. The appearance of the error page is controlled by the
error templates. There are two error templates: error/404 which is used for "404 Page Not Found" error, and error/index which is displayed when an unhandled exception is thrown somewhere inside of the application.
The module.config.php file contains several parameters under the view_manager key, which you can use to configure the appearance of your error templates:
<?php
return array(
//...
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
//...
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
//...
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index'=> __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
The display_not_found_reason parameter controls whether to display the detailed
information about the "Page not Found" error.
The display_exceptions parameter defines whether to display information about
an unhandled exception and its stack trace.
The not_found_template defines the template name for the 404 error.
The exception_template specifies the template name for the unhandled exception error.
You typically set the display_not_found_reason and display_exceptions parameters
to false in production systems, because you don't want site visitors see the details
about errors in your site. However, you will still be able to retrieve the detailed
information from Apache's error.log file.
Related
I try to change Css file for CGridView widget, in my config/main.php:
'components' => array(
'widgetFactory' => array(
'widgets' => array(
'CGridView' => array(
'cssFile' => Yii::app()->request->baseUrl . '/css/gridview.css',
),
),
),
...
And I get warning:
Trying to get property on a non object /path_to_project/protected/config/main.php on line 79
How I can suppress this warning, and why I getting it, in when I using it in view files it all works.
P.S. Yes I can set display_errors ini set to false and message will dissapearm but I want get clearly with it. Thanks!
The reason for the warning is that the CHttpRequest object Yii::app()->request has not been instantiated yet.
From the API page for CApplication:
CApplication will undergo the following lifecycles when processing a
user request:
load application configuration;
set up error handling;
load static application components;
onBeginRequest: preprocess the user request;
processRequest: process the user request;
onEndRequest: postprocess the user request;
Starting from lifecycle 3, if a PHP error or an uncaught exception occurs, the application will switch to its error handling logic and jump to step 6 afterwards.
Your error is happening at the first step. As such, Yii's error handling has not been setup yet. The only option is to suppress this warning using the # operator:
'cssFile' => #Yii::app()->request->baseUrl . '/css/gridview.css',
HOWEVER
This is a terrible idea, since you are essentially hiding the error instead of fixing it.
If your views are being displayed correctly (no css errors), you can omit the Yii::app()->request->baseUrl and just use:
'cssFile' => '/css/gridview.css'
If you are experiencing errors, you can create a class in your components folder that extends CWidgetFactory and set any variables that depend on other components here e.g
class MyWidgetFactory extends CWidgetFactory {
public function init() {
parent::init();
$this->widgets['CGridView']['cssFile'] = Yii::app()->request->baseUrl.'css/gridview.css';
}
}
You will need to adjust your components to use this file:
'components' => array(
'widgetFactory' => array(
'class' => 'MyWidgetFactory'
...
I would like to structure my project like the following:
MyModule/src/MyModule/Controller/TestController.php
MyModule/src/MyModule/Controller/Admin/TestController.php
The problem is, that both controllers look for the view file mymodule/test.phtml, because the directory Admin doesn't matter. Is there a way to take care of the directory without write it down manually in each controller action?
I would like to structure it like that:
MyModule/src/MyModule/Controller/TestController.php => view/mymodule/test.phtml
MyModule/src/MyModule/Controller/Admin/TestController.php => view/mymodule/admin/test.phtml
Or maybe someone has an idea to structure it otherwise?
Thanks
I've seen this done in a previous project before. Pretty sure you can config this in the module.config.php file, maybe something like:
// View file paths
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'MyModule/Admin/Test' => __DIR__ . '/../view/mymodule/admin/test.phtml'
)
)
Just make sure the paths are correct. However, you could just have an Admin module and have all of the admin controllers reside there that way you wouldn't have to worry about the Controller/Admin issue you are having with the view files right now. Then all of your other modules could either extend, use DI or the service mgr to get what you need from the Admin module.
You can override the default template injector with your own one where you will specify the logic that will resolve the template path for the admin controllers.
Check out my blog post covering this topic in depth with an example
http://blog.igorvorobiov.com/2014/10/18/creating-a-custom-template-injector-to-deal-with-sub-namespaces-in-zend-framework-2/
'errorHandler' => array(
'class' => 'ErrorHandler',
'errorAction' => 'page/find',
),
http://shot.qip.ru/008pAk-4IA4wMhU6/
I have standard error handling with beautiful error page. But for develop environment I need standard stacktrace on it below.
Examlpe: http://shot.qip.ru/008pAk-4IA4wMhU7/
If I comment 'errorAction' I can see just standart stacktrace, in other case I cant display this stacktrace.
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class' => 'CWebLogRoute',
'categories' => 'application, exception.*',
'levels'=>'error, warning, trace, profile, info',
'showInFireBug' => true,
'enabled' => YII_DEBUG,
),
array(
'class'=>'ext.yii-debug-toolbar.YiiDebugToolbarRoute',
'ipFilters'=>array('127.0.0.1','192.168.0.100'),
),
array(
'class'=>'CProfileLogRoute',
'report'=>'summary',
// Shows the execution time of each labeled with a code block.
// The value of "report" can also be specified as a "callstack".
),
),
),
Error handler by default uses two types of views for
Production named as error.php;
Development named as named as exception.php;
Based on your routing and error handler code. I see you have defined a custom error action
You will have to place your custom Errors views in either of the following folders, in the format specified in the link below and use the standard error action.
themes/ThemeName/views/system: when a theme is active.
protected/views/system
See this Documentation for detailed explanation
Reference: http://www.yiiframework.com/doc/api/1.1/CErrorHandler
Try this extenstion http://www.yiiframework.com/extension/yii-debug-toolbar/
The Yii Debug Toolbar is a configurable set of panels that display various debug information about the current request/response and when clicked, display more details about the panel's content.
It is a ported to PHP famous Django Debug Toolbar.
I need 2 different template maps in ZF2 , one for admin and oen for front-end, currently from what I can see ZF2 merges the 2 module.config.php files that are used in the 2 modules I configured, and causes the template map I need to set for the admin, to be loaded in front module also.
the /Application module.config.php
...
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
...
the /admin module.config.php
...
'view_manager' => array(
'template_path_stack' => array(
'admin' => __DIR__ . '/../view',
),
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
),
),
...
what should I modify so that i can load separate "view_manager" arrays for the 2 separate modules ?
It would be great if you can explain why you are trying to achieve this. As i can see you are trying to have a different layout for admin. Maybe you want to take a look at this module which can already do what you are trying https://github.com/zf-commons/zfcadmin. This module has a layut setup for the admin route.
I too had this problem. I took the approach which is proposed in the below module
https://github.com/EvanDotPro/EdpModuleLayouts
I'm new to ZF2 and I too was looking for an answer on how to have a completely different template for a dashboard, admin and front end.
I used EdpModuleLayouts as suggested here and in many other posts. This solved one aspect of the problem. The layout. I was now able to provide different layouts for the same template which would work well if I were using the same template or wanted to prove a different layout for say forgotten password, registration or a login. But I didn't. I needed a whole different set of folders, css files etc. At this point I could have just nested all templates in to a template folder and pointed the links in the layout files to the appropriate folders. But I didn't want this either.
I also included the zfc-admin module into my app which gave me a clue as to the other aspect which is to provide a different source directly for files. (Uninstalled afterward)
So adding by adding the following to my module_name/config/module.config.php
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view'
),
),
Enabling EdpModuleLayouts in application.config.php and adding the code below to the Application module.config.php
'module_layouts' => array(
'Application' => 'layout/layout',
'Dashboard' => 'layout/dashboard',
'Admin' => 'layout/admin',
),
This is probably not the best way to do it but it worked. The only issue I could really see with doing it this way is that EdpModuleLayouts wants to pull all the layouts from the Application/view/layout folder. It did however allow me to keep all my module template files in the view section of the module being worked on.
I've created a module, a basic copy of the the albums example given in the ZF2 documentation, however, with the new module, I am not able to access it at all - I'm always given a 404 error. I'm building this on the ZF2 skeleton.
I've got three modules loaded: Application, Frontend and Security.
Both Frontend and Security are duplicates of each other, however, I have thoroughly checked and there is no reference to old code (as I literally copied the module folder and renamed/rewrote references).
The module is also loaded in application.config.php.
Any ideas on what I'm missing?
Module Config:
return array(
'controllers' => array(
'invokables' => array(
'Security\Controller\Security' => 'Security\Controller\SecurityController',
),
),
'router' => array(
'routes' => array(
'security' => array(
'type' => 'segment',
'options' => array(
'route' => '/security[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Security\Controller\Security',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'security' => __DIR__ . '/../view',
),
),
);
I had the same problem while following the skeleton application tutorial (Getting started: A skeleton application). Whenever I would go to the album url in the browser (ZendSkeletonApplication/public/album in my case), I would get a 404 error page but no details on why I got the 404. It wasn't clear to me how I would be able determine why I was getting the 404 when I had double checked everything and was pretty sure I copied and configured the Album module properly. It turned out that I was missing a slash in my route (module.config.php). For example I had 'route' => 'album[/:action][/:id]' instead of 'route' => '/album[/:action][/:id]'.
I was only able to figure it out by intentionally causing errors by misspelling things like making the 'Album\Controller\Albums' instead of 'Album\Controller\Album'in the invokables value, this would cause a stack trace to display which then showed the ZF2 classes that where called on the request. I would continue to misspell, test, and then correct each part of the module.config.php until I was given a clue to what part of the configuration was causing the error.
I'm pretty sure this was not the best way to debug an application's configuration.
There is few things that need to be make sure is:-
You have to add your module in
application.config.php (which you are saying you done it.)
Security\Controller\Security has to be same in default too (which you already has)
One more thing is Your folder structure....
-
Just to doulbe check you have a /MODULE/src/MODULE/Controller/CONTROLLER_FILE_NAME.php
I hope that helps..
I know it is an old post. However another thing to make sure you have in the modules top directory (same directory as the Module.php file) is the "autoload_classmap.php"
file with "<?php return array();?>" inside of it.
A simple tip to know whether your rule has already added correctly to the routes or not, you may check the routes value in the config file inside any working module, as following:
$config = $this->serviceLocator->get('config');
var_dump($config);