Skip to content

Commit

Permalink
bug #37 Display a custom error page when the requested entity doesn't…
Browse files Browse the repository at this point in the history
… exist. (javiereguiluz)

This PR was merged into the master branch.

Discussion
----------

Display a custom error page when the requested entity doesn't exist.

This PR also introduces a render404error() shortcut to avoid the
ugly code required to render a template and returning a 404 Response.

This fixes #8

Commits
-------

5eb7791 Display a custom error page when the requested entity doesn't exist.
  • Loading branch information
javiereguiluz committed Jan 17, 2015
2 parents 3d24f03 + 5eb7791 commit 44942fd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@ protected function initialize(Request $request)
$this->config = $this->container->getParameter('easy_admin.config');

if (0 === count($this->config['entities'])) {
return $this->render('@EasyAdmin/error/no_entities.html.twig', array(), new Response('', 404));
return $this->render404error('@EasyAdmin/error/no_entities.html.twig');
}

if (!in_array($action = $request->query->get('action', 'list'), $this->allowedActions)) {
return $this->render('@EasyAdmin/error/forbidden_action.html.twig', array(
return $this->render404error('@EasyAdmin/error/forbidden_action.html.twig', array(
'action' => $action,
'allowed_actions' => $this->allowedActions,
), new Response('', 404));
));
}

if (null !== $entityName = $request->query->get('entity')) {
if (!array_key_exists($entityName, $this->config['entities'])) {
return $this->render404error('@EasyAdmin/error/undefined_entity.html.twig', array('entity_name' => $entityName));
}

$this->entity['name'] = $entityName;
$this->entity['class'] = $this->config['entities'][$entityName]['class'];
$this->entity['metadata'] = $this->getEntityMetadata($this->entity['class']);
Expand Down Expand Up @@ -454,7 +458,7 @@ protected function createNewForm($entity, array $entityFieldsMapping)
return $form->getForm();
}

private function getNameOfTheFirstConfiguredEntity()
protected function getNameOfTheFirstConfiguredEntity()
{
$entityNames = array_keys($this->config['entities']);

Expand All @@ -470,4 +474,9 @@ protected function createDeleteForm($entityName, $entityId)
->getForm()
;
}

protected function render404error($view, $parameters = array())
{
return $this->render($view, $parameters, new Response('', 404));
}
}
49 changes: 49 additions & 0 deletions Resources/views/error/undefined_entity.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="EasyAdmin" />

<title>EasyAdmin Error</title>

<link href="{{ asset('bundles/easyadmin/stylesheet/bootstrap.min.css') }}" rel="stylesheet">
<link href="{{ asset('bundles/easyadmin/stylesheet/font-awesome.min.css') }}" rel="stylesheet">
<link href="{{ asset('bundles/easyadmin/stylesheet/admin.css') }}" rel="stylesheet">
<link rel="shortcut icon" type="image/png" href="/favicon.png">
</head>

<body class="error">
<div id="wrapper" class="container">
<div id="header" class="col-lg-2">
<div id="header-contents" class="row">
<div id="header-logo" class="col-xs-6 col-md-2 col-lg-12">
<a href="{{ path('admin') }}">EasyAdmin</a>
</div>
</div>
</div>

<div id="content" class="col-lg-10 col-lg-offset-2">
<div class="row">
<div class="col-sm-9" id="main">
<h1>Runtime error</h1>
<p class="lead">
The <code>{{ entity_name }}</code> entity is not defined in
the configuration of your backend.
</p>

<h2>How to fix this problem</h2>

<p>
Open your <code>app/config/config.yml</code> file and add the
class of the <code>{{ entity_name }}</code> entity to the list
of entities managed by EasyAdmin.
</p>
</div>
</div>
</div>
</div>
</body>
</html>

0 comments on commit 44942fd

Please sign in to comment.