How can I have a custom layout for a 404 page in Cake? I know you can create your own view but I also want a custom layout for it as I don't want it inheriting my site design and want it to have a unique look and feel.
I've created my own views and then added my own app_error in /app/ with the following code:
<?php
class AppError extends ErrorHandler
{
function error()
{
$this->layout = 'error';
}
}
?>
But it doesn't load the error layout? Any ideas why?
Thanks.
Create your own AppError class (in app/app_error.php and override the _outputMessage method, something like:
class AppError extends ErrorHandler {
function _outputMessage($template) {
$this->controller->render($template, 'NAME OF THE LAYOUT');
$this->controller->afterFilter();
echo $this->controller->output;
}
}
I believe you just create a file at /views/errors/error404.thtml. It may end in .ctp actually, but give both a shot.
Create/edit the app/views/errors/error404.ctp page. See CakePHP Error Handling.
Related
Hi I'm pretty new to MVC framework. I decided to build a small application in MVC to get better understanding.
I googled around and found PIP framework that i can use for simple application and then modify once i will get complete understanding.
PIP can be found here
Now i have one question that by looking to PIP framework, do i need to design to new controller for every page that i will use.
For e.g
applications/
views/
home.php
about.php
contact.php
controllers/
main.php
aboutus.php ??
contactus.php ??
For eg. my default controller and view is main.php and home.php and i have a controller for main.php as below:
<?php
class Main extends Controller {
function index()
{
$template = $this->loadView('home');
$template->set('title', "Welcome Homepage");
$template->render();
}
}
?>
So, in this way, do i need to create new conrollers for about and contact.php
PIP's routing is internalized and requires a specific scaffolding of your controllers:
When you go to:
example.com/main
It looks for the controller main and then the function index by default.
If you were going to have 1 controller per view, then it would be something like:
example.com/main
class Main extends Controller
example.com/about
class About extends Controller
example.com/contact
class Contact extends Controller
However, if you went with a default page controller, this would greatly simplify the scaffolding:
example.com/page/{about|contact|main}
class Page extends Controller {
public function about (){
}
public function contact(){
}
public function main(){
}
}
Now 1 controller will handle the delivery of each of these pages.
With pip the URL defines the controller and function that will be executed. If you want the contact page to be at mysite.com/contact, you do need to add a Contact controller.
You do not need to create a new controller for every page. Lets assume you had a forum. You could have the following urls:
mysite.com/blog/write
mysite.com/blog/view/15
These different URLs would all be handled in one controller, blog.php
<?php
class Blog extends Controller {
public function write()
{
echo 'Hello World!';
}
public function view($postId)
{
echo 'Viewing post: ' . $postId;
}
}
I also used PIP as a starter framework, and extended it to include some additional functionality. I even started to document my efforts, but other projects have taken me in other directions.
Happy to share what I have, and some documentation on my PIP efforts can be found at http://www.shed22.com/pip.
Let me know if you would like a copy of the source code and a sample application.
Stephen
I am trying to hide the 'help' button on the dashboard by override. But the override is not being used. So I was wondering what am I doing wrong? Why is the override not used?
I found it in: classes/controller/AdminController.php. Then I created a new file: override/classes/controller/AdminController.php.
I could not get the override to work, so I tried to check if it was taken into account at all by:
<?php
class AdminControllerCoreOverride extends AdminControllerCore
{
echo 'askdjfkdjfksl';
}
?>
But nothing happened. I deleted cache: index_cache and the override is not turned off in performance menu. In the index_cache.php I found the AdminController but override was false.
PS: using Prestashop 1.6
If you create an override file manually, you must delete the file cache/class_index.php for your override file to work.
Then, in override/classes/controller/AdminController.php you must override a function like so:
<?php
class AdminController extends AdminControllerCore
{
public function initPageHeaderToolbar()
{
Your code
}
}
You don't need to put ?> at this end of this file.
I faced similar issue (with PS7). In my case the problem was caused by file permission.
Prestashop needs write permision to the file you want to override. Otherwise the file is ignored without any warning/error message. You can delete class_index.php file like crazy with no help.
Also, for some reason, I had to reset my module anytime I do anychange in my override controller.
BTW in PS7 the cache_index is located under /app/cache/dev folder (and /app/cache/prod folder).
you can try this:
update your class name from AdminCoreControllerOverride to AdminCoreOverrideController
and AdminControllerCore to AdminController
<?php
class AdminCoreOverrideController extends AdminController
{
public function init() {
parent::init();
}
}
then you can test in your browser by this link:
http://localhost/YOUR_ADMIN_DIR/index.php?controller=AdminCoreOverride
I have following code:
class MY_Router extends CI_Router {
function __construct() {
parent::__construct();
log_message('debug', "My Child Router Class is here");
} }
I have run the web page and saw log, but didn't find any log message there.
My purpose is that, instaed of default router, I want to use custom made router.
Can some one guide me what and where Iam doing wrong. And how it can be rectified.
Thanks in advance
If you are simply testing to see if the class if being constructed properly, it may be easier to just call an error message. If the error message displays then you're good to go.
show_error("My child router class is up and running.");
Also, ensure that your file is named "MY_Router.php" and is placed in your /application/core directory.
The user guide entry relating to extending core classes can be found at http://codeigniter.com/user_guide/general/core_classes.html
I have this custom error handler:
`class AppError extends ErrorHandler {
function error404($params) {
$this->controller->layout = 'public';
$this->controller->set('title','Droptor Page Not Found');
parent::error404($params);
}
}`
And I can't seem to use any layout that has this:
$javascript->link('jquery',true)
So the JS helper isn't loaded. But if I include this in the controller: var $helpers = array('javascript'); it still doesn't work. Nor does App::import('Helper', 'javascript');
Crap, I didn't read your question.
To add a helper to your error controller, just add this line:
$this->controller->helpers = array('Javascript');
There are two ways to do it:
First, you can create an app_controller to include every component and helper that you need on all your controllers.
Second, you can load the specific resources needed to your error controller. Create a file named error.php in your app's root (NOT webroot) with the following code:
<?php
class AppError extends ErrorHandler {
function error404($params) {
$this->controller->helpers = array('Javascript');
parent::error404($params);
}
}
You can also set a custom title with
$this->controller->set('title_for_layout', "We couldn't find what you are loooking for");
Good luck.
First off, you don't need to create your own handler if all you're doing is handling a common error type, such as 404. Custom error handlers are for your application specific errors.
If you want to simply change the layout of your page when you get a 404 error, this has been answered over here.
function beforeRender() {
if($this->name == 'CakeError') {
$this->layout = false;
}
}
And you can cause it using the line:
$this->cakeError('error404');
I had been wondering why my error page caused certain pages of my site
not to render, but then I realized that it's because AppError extends
ErrorHandler instead of AppController. This caused some variables
that I set in AppController's beforeFilter method not to be sent to
the view. Since I can't access session variables from AppError, I
thought that I might be able to get away with using the classRegistry
to instantiate something that could and simply copying and pasting the
rest of my code from AppController's beforeFilter... but that isn't working, nor does it seem like a very elegant fix. Does anyone have any clues as to what
would be the best way to approach this? Thanks, David.
Your AppError class has a controller instance. You can call the beforeFilter manually:
<?php
class AppError extends ErrorHandler {
function error404() {
$this->controller->beforeFilter();
parent::error404();
}
}
?>
In CakePHP 2, you can do something like this to achieve the same effect. In app/Config/bootstrap.php, add this line:
Configure::write('Exception.renderer', 'AppExceptionRenderer');
Then create a file app/Lib/Error/AppExceptionRenderer.php with this code:
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
protected function _outputMessage($template) {
$this->controller->beforeFilter();
$this->controller->render($template);
$this->controller->afterFilter();
$this->controller->response->send();
}
}
Described more generally here: http://book.cakephp.org/2.0/en/development/exceptions.html#using-a-custom-renderer-with-exception-renderer-to-handle-application-exceptions
Edit: Updated link to point to correct location of the CakePHP 2.0 Book as of July 05, 2012.