The HTTP Request provides an object oriented interface to global variables available to our disposal as well as easy access to other useful request information.
We have learned how to make controller and route work, now it's time to see how the application receives HTTP request.
The getRequest() method returns all the available object oriented interface to global variables such as $_GET, $_POST, $_FILES, $_COOKIE and $_SERVER.
namespace namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
/**
* Class IndexController
* @package Application\Controller
*/
class IndexController extends AbstractController
{
/**
* @return ViewModel
*/
public function indexAction()
{
// Get all the available object oriented interface of global variables
$request = $this->http->getRequest();
// Pass a variable to the view
return new ViewModel([
'foobar' => 'Hello World!'
]);
}
}
Now lets explore each ways of accessing the different kind of request data.
The getQuery() method returns a parameter collection containing query string data.
namespace namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
/**
* Class IndexController
* @package Application\Controller
*/
class IndexController extends AbstractController
{
/**
* @return ViewModel
*/
public function indexAction()
{
// Get all the available object oriented interface of global variables
$request = $this->http->getRequest();
// Returns a parameter collection containing query string data
$queryString = $request->getQuery();
// Pass a variable to the view
return new ViewModel([
'foobar' => 'Hello World!'
]);
}
}
The get() method of the parameter collection returns the value of a parameter and null if it doesn't exist.
// Return the value of a parameter and null if the key doesn't exist
$data = $queryString->get('foo');
// You can also specify a custom default return value if the key doesn't exist using the second parameter
$data = $queryString->get('foo', 'bar');
The getPost() method returns a parameter collection containing post data.
namespace namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
/**
* Class IndexController
* @package Application\Controller
*/
class IndexController extends AbstractController
{
/**
* @return ViewModel
*/
public function indexAction()
{
// Get all the available object oriented interface of global variables
$request = $this->http->getRequest();
// Returns a parameter collection containing post data
$post = $request->getPost();
// Pass a variable to the view
return new ViewModel([
'foobar' => 'Hello World!'
]);
}
}
The get() method of the parameter collection returns the value of a parameter and null if it doesn't exist.
// Return the value of a parameter and null if the key doesn't exist
$data = $post->get('foo');
// You can also specify a custom default return value if the key doesn't exist using the second parameter
$data = $post->get('foo', 'bar');
The getCookie() method returns a parameter collection containing cookie data.
namespace namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
/**
* Class IndexController
* @package Application\Controller
*/
class IndexController extends AbstractController
{
/**
* @return ViewModel
*/
public function indexAction()
{
// Get all the available object oriented interface of global variables
$request = $this->http->getRequest();
// Returns a parameter collection containing cookie data
$cookie = $request->getCookie();
// Pass a variable to the view
return new ViewModel([
'foobar' => 'Hello World!'
]);
}
}
The get() method of the parameter collection returns the value of a parameter and null if it doesn't exist.
// Return the value of a parameter and null if the key doesn't exist
$data = $cookie->get('foo');
// You can also specify a custom default return value if the key doesn't exist using the second parameter
$data = $cookie->get('foo', 'bar');
The getFile() method returns a parameter collection containing file data.
namespace namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
/**
* Class IndexController
* @package Application\Controller
*/
class IndexController extends AbstractController
{
/**
* @return ViewModel
*/
public function indexAction()
{
// Get all the available object oriented interface of global variables
$request = $this->http->getRequest();
// Returns a parameter collection containing file data
$file = $request->getFile();
// Pass a variable to the view
return new ViewModel([
'foobar' => 'Hello World!'
]);
}
}
The get() method of the parameter collection returns the value of a parameter and null if it doesn't exist.
// Return the value of a parameter and null if the key doesn't exist
$data = $file->get('foo');
The getServer() method returns a parameter collection containing server data.
namespace namespace Application\Controller;
use JiNexus\Mvc\Controller\AbstractController;
use JiNexus\Mvc\Model\ViewModel;
/**
* Class IndexController
* @package Application\Controller
*/
class IndexController extends AbstractController
{
/**
* @return ViewModel
*/
public function indexAction()
{
// Get all the available object oriented interface of global variables
$request = $this->http->getRequest();
// Returns a parameter collection containing server data
$server = $request->getServer();
// Pass a variable to the view
return new ViewModel([
'foobar' => 'Hello World!'
]);
}
}
The get() method of the parameter collection returns the value of a parameter and null if it doesn't exist.
// Return the value of a parameter and null if the key doesn't exist
$data = $server->get('foo');
These are the list of available parameter methods that can be use from request.
Method | Description |
---|---|
count() | Return the count of all parameter |
add() | Adds a parameter |
has() | Returns true if the parameter exists and false if not |
get() | Gets a parameter value |
remove() | Removes a parameter |
all() | Returns all the parameter |
getIterator() | Returns an array iterator object |
The isAjax() method returns true if the request was made using AJAX and false if not.
$isAjax = $this->request->isAjax();
The isSecure() method returns true if the request was made using HTTPS and false if not.
$isSecure = $this->request->isSecure();
The baseUrl() method returns the base URL of the request.
$baseUrl = $this->request->baseUrl();
If you found a typo or error, please help us improve this document. Contact Us