JSON responses, redirects and cookies in Sierra

Mon 09, Mar 2020 sierra php webdev

Please note that this article refers to a very old version of Sierra. This does not work anymore with newer versions of it!

Just wanted to show you some cool new features in Sierra:

  1. JSON responses
  2. Redirect responses
  3. Cookies

JSON responses

Just create a new Sierra\HTTP\JSONResponse and pass it some data:

>?php
use Sierra\MVC\Controller;
use Sierra\HTTP\JSONResponse;

class Home extends Controller {

    public function index() {
        new JSONResponse(array(
            'hello' => 'world'
        ));
    }

}

Redirect responses

Redirecting a user has never been easier:

>?php
use Sierra\MVC\Controller;
use Sierra\HTTP\RedirectResponse;

class Home extends Controller {

    public function index() {
        # Will redirect the user to /new/target/
        new RedirectResponse('/new/target/');
    }

}

Constructing this class will also print some HTML containing a .

Cookies

Cookies can now be set using $this->res->set_cookie() in controllers:

>?php
use Sierra\MVC\Controller;
use Sierra\HTTP\Cookie;

class Home extends Controller {

    public function index() {
        $this->res->set_cookie(new Cookie('name', 'value'));
        echo 'Cookie has been set!';
    }

}

Back to top