mirror of
https://github.com/flarum/framework.git
synced 2025-02-21 08:39:25 +08:00
Move view logic into middleware
This commit is contained in:
parent
e10baaaf62
commit
6035615660
@ -12,11 +12,13 @@
|
||||
namespace Flarum\Http\Middleware;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Franzl\Middleware\Whoops\ErrorMiddleware as WhoopsMiddleware;
|
||||
use Illuminate\Contracts\View\Factory as ViewFactory;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
|
||||
class HandleErrors
|
||||
@ -31,6 +33,16 @@ class HandleErrors
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
@ -41,10 +53,12 @@ class HandleErrors
|
||||
* @param LoggerInterface $logger
|
||||
* @param bool $debug
|
||||
*/
|
||||
public function __construct(ViewFactory $view, LoggerInterface $logger, $debug = false)
|
||||
public function __construct(ViewFactory $view, LoggerInterface $logger, TranslatorInterface $translator, SettingsRepositoryInterface $settings, $debug = false)
|
||||
{
|
||||
$this->view = $view;
|
||||
$this->logger = $logger;
|
||||
$this->translator = $translator;
|
||||
$this->settings = $settings;
|
||||
$this->debug = $debug;
|
||||
}
|
||||
|
||||
@ -89,8 +103,29 @@ class HandleErrors
|
||||
$name = 'flarum::error.default';
|
||||
}
|
||||
|
||||
$view = $this->view->make($name)->with('error', $error);
|
||||
$view = $this->view->make($name)
|
||||
->with('error', $error)
|
||||
->with('message', $this->getMessage($status));
|
||||
|
||||
return new HtmlResponse($view->render(), $status);
|
||||
}
|
||||
|
||||
private function getMessage($status)
|
||||
{
|
||||
if (! $translation = $this->getTranslationIfExists($status)) {
|
||||
if (! $translation = $this->getTranslationIfExists(500)) {
|
||||
$translation = 'An error occurred while trying to load this page.';
|
||||
}
|
||||
}
|
||||
|
||||
return $translation;
|
||||
}
|
||||
|
||||
private function getTranslationIfExists($status)
|
||||
{
|
||||
$key = 'core.views.error.'.$status.'_message';
|
||||
$translation = $this->translator->trans($key, ['{forum}' => $this->settings->get('forum_title')]);
|
||||
|
||||
return $translation === $key ? false : $translation;
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
@section('content')
|
||||
<p>
|
||||
{{ $translator->trans('core.views.error.404_message') }}
|
||||
{{ $message }}
|
||||
</p>
|
||||
<p>
|
||||
<a href="{{ $app->url() }}">
|
||||
|
@ -2,21 +2,6 @@
|
||||
|
||||
@section('content')
|
||||
<p>
|
||||
{{-- TODO: Change below to @php when Laravel is upgraded --}}
|
||||
<?php
|
||||
$getTranslationIfExists = function ($code) use ($translator, $settings) {
|
||||
$key = 'core.views.error.'.$code.'_message';
|
||||
$translation = $translator->trans($key, ['{forum}' => $settings->get('forum_title')]);
|
||||
|
||||
return $translation === $key ? false : $translation;
|
||||
};
|
||||
|
||||
if (! $translation = $getTranslationIfExists($error->getCode())) {
|
||||
if (! $translation = $getTranslationIfExists(500)) {
|
||||
$translation = 'An error occurred while trying to load this page.';
|
||||
}
|
||||
}
|
||||
?>
|
||||
{{ $translation }}
|
||||
{{ $message }}
|
||||
</p>
|
||||
@endsection
|
||||
|
Loading…
x
Reference in New Issue
Block a user