Lithium forward request

2019-03-22 04:48发布

The controller redirect() method in Lithium does an actual HTTP redirect. But is there a method to simply forward a request to another controller/action without an HTTP redirect?

For example, say I want to add an authentication layer and rather than redirecting the user to a "/auth/login" page, the login layout and template get rendered rather than the content for the page they requested. Then, when they submit the form and they authenticate, they're already on the page they requested. Zend framework has something similar with a _forward() method.

Thanks!

1条回答
2楼-- · 2019-03-22 05:23

There's no method, mostly because you can do it in about two lines of code:

<?php

    namespace my_app\controllers;

    use lithium\core\Libraries;

    class PostsController extends \lithium\action\Controller {

        public function index() {
            $forward = Libraries::instance("controllers", "Auth", [
                'request' => $this->request
            ]);
            return $forward($this->request, ['action' => 'login']);
        }
    }
?>
查看更多
登录 后发表回答