1

たとえば、このアプリで手動ブートストラップの 404 エラー ページを作成する方法は? http://album-o-rama.phalconphp.com/

私はこのディスパッチャを使用します:

$di->set(
'dispatcher',
function() use ($di) {

    $evManager = $di->getShared('eventsManager');

    $evManager->attach(
        "dispatch:beforeException",
        function($event, $dispatcher, $exception)
        {
            switch ($exception->getCode()) {
                case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward(
                        array(
                            'controller' => 'error',
                            'action'     => 'show404',
                        )
                    );
                    return false;
            }
        }
    );
    $dispatcher = new PhDispatcher();
    $dispatcher->setEventsManager($evManager);
    return $dispatcher;
},
true

);

4

4 に答える 4

4

index.php でこれを試してください:

$di->set('dispatcher', function() {

    $eventsManager = new \Phalcon\Events\Manager();

    $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {

        //Handle 404 exceptions
        if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
            $dispatcher->forward(array(
                'controller' => 'index',
                'action' => 'show404'
            ));
            return false;
        }

        //Handle other exceptions
        $dispatcher->forward(array(
            'controller' => 'index',
            'action' => 'show503'
        ));

        return false;
    });

    $dispatcher = new \Phalcon\Mvc\Dispatcher();

    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;

}, true);
于 2014-06-27T07:31:49.013 に答える
1

ここで推奨される機能は次のとおりです。

http://docs.phalconphp.com/en/latest/reference/routing.html#not-found-paths

そして多分

routing.html#dealing-with-extra-trailing-slashes

手動ブートストラップの場合、ディスパッチャーを使用する代わりに、ルーターを設定できます

/**
 * Registering a router
 */
$di->set('router', require __DIR__.'/../common/config/routes.php');

次に、このルート ルールを「common/config/routes.php」に追加します。

$router->notFound(array(
    'module' => 'frontend',
    'namespace' => 'AlbumOrama\Frontend\Controllers\\',
    'controller' => 'index',
    'action' => 'route404'
));

最後に、このアクションをキャプチャするコントローラーとビューを定義します。

そしてほら、404エラーページ!

コメントとして、あなたが言及したアプリのこのソリューションをプルリクエストします。

https://github.com/phalcon/album-o-rama/pull/5/files

于 2014-07-01T02:28:44.230 に答える
1

の新しいバージョンではPhalcon、このコードをに追加することで、ルートを使用してエラーを処理できますservice.php

$di->set('router',function() use($Config){
    $router = new \Phalcon\Mvc\Router();
    $router->notFound(array(
        "controller" => "error",
        "action" => "error404"
    ));
    return $router;
}); 
于 2015-04-20T13:04:17.027 に答える
-2
public function show404Action()
{
    $this->response->setStatusCode(404, 'Not Found');
    $this->view->pick('error/show404');
}
于 2014-06-28T18:07:29.467 に答える