17560

CakePHP AppError extend functions

Question:

I have the following code as my AppError file on my CakePHP site:

<?php class AppError extends ErrorHandler { function _outputMessage($template) { $this->controller->render($template, 'error'); $this->controller->afterFilter(); echo $this->controller->output; } function error404 () { $this->set('title_for_layout', 'Not Found (404)'); } } ?>

The first function works fine by making all errors use the error layout instead of the default one. But the second function causes the App to blow up... why? Shouldn't it just extend the error404 function inside the error handler?

Thanks

Answer1:

It extends the class and by that AppError inherits all the methods from ErrorHandler.So if you don't have a method error404() , the AppError object will call its parent method, in this case error404 in ErrorHandler;

However, if you define the method that already exits in ErrorHandler it will not 'extend it' but 'override it.' In other words: If you create method error404 and this method is called by AppError object it will call its version of error404()

Now, if you look at error404 function inside ErrorHandler

function error404($params) { extract($params); if (!isset($url)) { $url = $action; } if (!isset($message)) { $message = ''; } if (!isset($base)) { $base = ''; } header("HTTP/1.0 404 Not Found"); $this->error(array('code' => '404', 'name' => 'Not found', 'message' => sprintf("The requested address %s was not found on this server.", $url, $message), 'base' => $base)); exit(); }

you can see that there is certain behavior and params are expected in this function. So that's why your app is crashing, somewhere AppError is calling error404 expecting the behavior from parent (ErrorHandler) class.Try to mimic the method (by including params and exit at the end).

Also $this->set('title_for_layout', 'Not Found (404)'); I think it should be

$this->controller->set('title_for_layout', 'Not Found (404)');`

Recommend

  • replace ? by / in urls and make them more friendly
  • Multiple instances of Service across AppDomains
  • Symfony 2 error page response
  • uml Composition relationships to RDF and OWL
  • Azure table store snapshot/backup capability
  • How to revert to previous XCode version?
  • xtable package: Skipping some rows in the output
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Meteor helpers not available in Angular template
  • Change JButton Shape while respecting Look And Feel
  • Can I display google adwords (AdView) in javafx on android
  • Align navbar back button on right side
  • Sony Xperia Z Tablet not found by adb
  • How to add date and time under each post in guestbook in google app engine
  • How to recover from a Spring Social ExpiredAuthorizationException
  • How to set/get protobuf's extension field in Go?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Validaiting emails with Net.Mail MailAddress
  • ILMerge & Keep Assembly Name
  • Where to put my custom functions in Wordpress?
  • sending/ receiving email in Java
  • Large data - storage and query
  • Web-crawler for facebook in python
  • How can I estimate amount of memory left with calling System.gc()?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • align graphs with different xlab
  • Akka Routing: Reply's send to router ends up as dead letters
  • WOWZA + RTMP + HTML5 Playback?
  • Numpy divide by zero. Why?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Hits per day in Google Big Query
  • Java static initializers and reflection
  • Qt: Run a script BEFORE make
  • python draw pie shapes with colour filled
  • unknown Exception android
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • Unable to use reactive element in my shiny app
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize