TetherPHP

Responders

Returning a View

In TetherPHP, you can return a view directly from your action or route definition. This is useful for static pages or simple responses.

$router->view('/path', 'pages.home');

This will render the view located at app/Views/pages/home.php.

Alternatively, from a Responder you can return views like so:

namespace Responders;

class ExampleResponder extends Responder
{
    public function __invoke(): string
    {
        return $this->response()->view('path.to.view');
    }
}
                    

Returning JSON

To return a JSON response, you can use the json method of the response object. This is particularly useful for APIs or AJAX requests.

namespace Responders;

class ExampleResponder extends Responder
{
    public function __invoke(): string
    {
        return $this->response()->json([
            'status' => 'success',
        ]);
    }
}