Author - Navneet Kashyap Post Views - 22 views

PHP – Application Controller

The Application Controller pattern is a design pattern used to centrally handle incoming requests in a web application. It helps to organize and manage how different parts of an application respond to various HTTP requests.

The main idea behind the Application Controller pattern is to have a single entry point that receives incoming requests, processes them, and delegates control to specific handlers or controllers based on the request type or route.

Here’s an example illustrating the Application Controller pattern in PHP:

$requestUri = $_SERVER[‘REQUEST_URI’];

// Simulating different routes
if ($requestUri === ‘/login) {
$controller = new LoginController();
} else if ($requestUri === ‘/dashboard) {
$controller = new DashboardController();
} else {
$controller = new ErrorController();


In the above example:

    1. The `$requestUri` variable fetches the URI from the server, simulating the route being accessed.
    2. Based on the requested URI, the code instantiates the appropriate controller (`LoginController`, `DashboardController`, or `ErrorController`) responsible for handling the request.
    3. Each controller class typically contains methods (e.g., `handle()`) to process the request, interact with models, and pass data to views.

Here’s an example of what the controller classes might look like:

// LoginController.php
class LoginController {
public function handle() {
// Logic specific to handling the ‘login’ route
echo “Welcome to the login screen!”;

// DashboardController.php
class DashboardController {
public function handle() {
// Logic specific to handling the ‘dashboard’ route
echo “Welcome to the Dashboard!”;

// ErrorController.php
class ErrorController {
public function handle() {
// Logic for handling errors or invalid routes
echo “404 – Page Not Found”;

In this example, the Application Controller pattern allows for a centralized approach to handling different routes or requests within the application by delegating control to specific controllers. Each controller encapsulates the logic related to a particular route, enhancing the modularity and maintainability of the application.

Leave a Reply

Your email address will not be published. Required fields are marked * cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri