getting started  »  controllers

Controllers

The Pop PHP Framework comes with a base abstract controller class Pop\Controller\AbstractController that can be extended to create the controller classes needed for your application. When writing your controller classes by extending the abstract controller class, you can define the methods that represent the actions that will be executed on the matched route. If you choose not to use the provided abstract controller class, you can write your own, but it needs to implement the controller interface Pop\Controller\ControllerInterface at a minimum.

HTTP Example

Here's an example of what a basic controller might look like for a web application:

namespace MyApp\Controller;

use Pop\Controller\AbstractController;
use Pop\Http\Server\Request;
use Pop\Http\Server\Response;

class IndexController extends AbstractController
{
    protected $request;
    protected $response;

    public function __construct(Request $request, Response $response)
    {
        $this->request  = $request;
        $this->response = $response;
    }

    public function index()
    {
        // Show the index page
    }

    public function products()
    {
        // Show the products page
    }

}

The above example uses the popphp/pop-http component and injects a server request and a response object into the controller's constructor. For more on how to inject controller parameters into the controller's constructor, refer the section on controller parameters under Routing.

CLI Example

For a console application, your controller class might look like this example below, utilizing the popphp/pop-console component and injecting a console object:

namespace MyApp\Controller;

use Pop\Controller\AbstractController;
use Pop\Console\Console;

class ConsoleController extends AbstractController
{
    protected $console;

    public function __construct(Console $console)
    {
        $this->console = $console;
    }

    public function home()
    {
        // Show the home screen
    }

    public function users()
    {
        // Show the users screen
    }

}