Overview

Namespaces

  • Charcoal
    • App
      • Action
      • Config
      • Handler
      • Middleware
      • Module
      • Route
      • Script
      • ServiceProvider
      • Template

Classes

  • Charcoal\App\Action\AbstractAction
  • Charcoal\App\App
  • Charcoal\App\AppConfig
  • Charcoal\App\AppContainer
  • Charcoal\App\Config\CacheConfig
  • Charcoal\App\Config\DatabaseConfig
  • Charcoal\App\Config\FilesystemConfig
  • Charcoal\App\Config\LoggerConfig
  • Charcoal\App\Config\MemcacheCacheConfig
  • Charcoal\App\Config\MemcacheCacheServerConfig
  • Charcoal\App\Handler\AbstractHandler
  • Charcoal\App\Handler\Error
  • Charcoal\App\Handler\HandlerConfig
  • Charcoal\App\Handler\NotAllowed
  • Charcoal\App\Handler\NotFound
  • Charcoal\App\Handler\PhpError
  • Charcoal\App\Handler\Shutdown
  • Charcoal\App\Middleware\CacheMiddleware
  • Charcoal\App\Module\AbstractModule
  • Charcoal\App\Module\ModuleConfig
  • Charcoal\App\Module\ModuleManager
  • Charcoal\App\Route\ActionRoute
  • Charcoal\App\Route\ActionRouteConfig
  • Charcoal\App\Route\RouteConfig
  • Charcoal\App\Route\RouteManager
  • Charcoal\App\Route\ScriptRoute
  • Charcoal\App\Route\ScriptRouteConfig
  • Charcoal\App\Route\TemplateRoute
  • Charcoal\App\Route\TemplateRouteConfig
  • Charcoal\App\Script\AbstractScript
  • Charcoal\App\ServiceProvider\AppServiceProvider
  • Charcoal\App\ServiceProvider\CacheServiceProvider
  • Charcoal\App\ServiceProvider\DatabaseServiceProvider
  • Charcoal\App\ServiceProvider\FilesystemServiceProvider
  • Charcoal\App\ServiceProvider\LoggerServiceProvider
  • Charcoal\App\ServiceProvider\ViewServiceProvider
  • Charcoal\App\Template\AbstractTemplate
  • Charcoal\App\Template\AbstractWidget
  • Charcoal\App\Template\WidgetBuilder

Interfaces

  • Charcoal\App\Action\ActionInterface
  • Charcoal\App\AppAwareInterface
  • Charcoal\App\Handler\HandlerInterface
  • Charcoal\App\Module\ModuleInterface
  • Charcoal\App\Route\RouteInterface
  • Charcoal\App\Script\CronScriptInterface
  • Charcoal\App\Script\ScriptInterface
  • Charcoal\App\Template\TemplateInterface
  • Charcoal\App\Template\WidgetInterface

Traits

  • Charcoal\App\AppAwareTrait
  • Charcoal\App\CallableResolverAwareTrait
  • Charcoal\App\Script\ArgScriptTrait
  • Charcoal\App\Script\CronScriptTrait
  • Charcoal\App\Script\PathScriptTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\App\Handler;
  4: 
  5: // Dependencies from PSR-7 (HTTP Messaging)
  6: use Psr\Http\Message\ServerRequestInterface;
  7: use Psr\Http\Message\ResponseInterface;
  8: 
  9: // Dependency from Slim
 10: use Slim\Http\Body;
 11: 
 12: // Local Dependencies
 13: use Charcoal\App\Handler\AbstractHandler;
 14: 
 15: /**
 16:  * Not Found Handler
 17:  *
 18:  * Enhanced version of {@see \Slim\Handlers\NotAllowed}.
 19:  *
 20:  * It outputs a simple message in either JSON, XML,
 21:  * or HTML based on the Accept header.
 22:  */
 23: class NotFound extends AbstractHandler
 24: {
 25:     /**
 26:      * Invoke "Not Found" Handler
 27:      *
 28:      * @param  ServerRequestInterface $request  The most recent Request object.
 29:      * @param  ResponseInterface      $response The most recent Response object.
 30:      * @return ResponseInterface
 31:      */
 32:     public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 33:     {
 34:         $contentType = $this->determineContentType($request);
 35:         switch ($contentType) {
 36:             case 'application/json':
 37:                 $output = $this->renderJsonOutput();
 38:                 break;
 39: 
 40:             case 'text/xml':
 41:             case 'application/xml':
 42:                 $output = $this->renderXmlOutput();
 43:                 break;
 44: 
 45:             case 'text/html':
 46:             default:
 47:                 $output = $this->renderHtmlOutput();
 48:         }
 49: 
 50:         $body = new Body(fopen('php://temp', 'r+'));
 51:         $body->write($output);
 52: 
 53:         return $response->withStatus(404)
 54:                         ->withHeader('Content-Type', $contentType)
 55:                         ->withBody($body);
 56:     }
 57: 
 58:     /**
 59:      * Render Plain/Text Error
 60:      *
 61:      * @return string
 62:      */
 63:     protected function renderPlainOutput()
 64:     {
 65:         $message = $this->translator()->translate('Not Found');
 66: 
 67:         return $this->render($message);
 68:     }
 69: 
 70:     /**
 71:      * Render JSON Error
 72:      *
 73:      * @return string
 74:      */
 75:     protected function renderJsonOutput()
 76:     {
 77:         $message = $this->translator()->translate('Not Found');
 78: 
 79:         return $this->render('{"message":"'.$message.'"}');
 80:     }
 81: 
 82:     /**
 83:      * Render XML Error
 84:      *
 85:      * @return string
 86:      */
 87:     protected function renderXmlOutput()
 88:     {
 89:         $message = $this->translator()->translate('Not Found');
 90: 
 91:         return $this->render('<root><message>'.$message.'</message></root>');
 92:     }
 93: 
 94:     /**
 95:      * Render title of error
 96:      *
 97:      * @return string
 98:      */
 99:     public function messageTitle()
100:     {
101:         return $this->translator()->translate('Page Not Found');
102:     }
103: 
104:     /**
105:      * Render body of HTML error
106:      *
107:      * @return string
108:      */
109:     public function renderHtmlMessage()
110:     {
111:         $title = $this->messageTitle();
112:         $link  = sprintf(
113:             '<a href="%1$s">%2$s</a>',
114:             $this->baseUrl(),
115:             $this->translator()->translate('Visit the Home Page')
116:         );
117:         $notice  = $this->translator()->translate('The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.');
118:         $message = '<h1>'.$title."</h1>\n\t\t<p>".$notice."</p>\n\t\t<p>".$link."</p>\n";
119: 
120:         return $message;
121:     }
122: }
123: 
API documentation generated by ApiGen