-
Notifications
You must be signed in to change notification settings - Fork 0
Response object
Every controller and middleware is passed an instance of alvinlal\phpmvc\Response. This object can be used to send data back to the client. It also contains methods for handling cookie and json.
This method is used to set http statusCode. Accepts http status code as argument.
// controllers/SiteController.php
<?php
namespace app\controllers;
use alvin\phpmvc\Controller;
class SiteController extends Controller {
public function protectedRoute(Request $request,Response $response) {
$response->statusCode(401);
return "unauthorized";
}
}
This method is used to redirect the client to another url. Accepts the redirect location url as argument.
// controllers/SiteController.php
<?php
namespace app\controllers;
use alvin\phpmvc\Controller;
class SiteController extends Controller {
public function redirectToSomewhere(Request $request,Response $response) {
$response->redirect('/posts');
}
}
This method sends a json response to the client.
Arguments
:-
1.obj:- object to send.
2.flags:- See [here](https://www.php.net/manual/en/json.constants.php) for all available flags. default is 0.
3.depth:- Maximum nesting depth of the structure being decoded. default is 512.
Sets a cookie on the client, Accepts a key, its value and options object.
If output exists prior to calling this function, setCookie() will fail and return false. If setCookie() successfully runs, it will return true. This does not indicate whether the user accepted the cookie.
options object : - An associative array which may have any of the keys expires, path, domain, secure, httponly and samesite. If any other key is present an error of level E_WARNING is generated. The values have the same meaning as described for the parameters with the same name. The value of the samesite element should be either None, Lax or Strict. If any of the allowed options are not given, their default values are the same as the default values of the explicit parameters. If the samesite element is omitted, no SameSite cookie attribute is set.
See here for more info on cookie options.
// controllers/SiteController.php
<?php
namespace app\controllers;
use alvin\phpmvc\Controller;
class SiteController extends Controller {
public function login(Request $request,Response $response) {
// do login stuff
$response->setCookie('token','randomtoken',['secure' => true]);
}
}
Deletes a cookie from the client. Accepts key of the cookie to be deleted.
// controllers/SiteController.php
<?php
namespace app\controllers;
use alvin\phpmvc\Controller;
class SiteController extends Controller {
public function logout(Request $request,Response $response) {
// do login stuff
$response->deleteCookie('token');
}
}
Sends a string to the client. Accepts string to send as argument.
👉 Currently executing script will end after executing this function.
same as returning a string from the controller
// controllers/SiteController.php
<?php
namespace app\controllers;
use alvin\phpmvc\Controller;
class SiteController extends Controller {
public function sayHello(Request $request,Response $response) {
$response->send("hello");
}
}