Hello world

Let's start the core tutorial in a traditional way: The Hello World application.

This example will implement a very minimal BlueMvc-application and inspect it piece by piece.

For this, the core package is required.

$ composer require bluemvc/bluemvc-core

For a real project you probably want to begin with a more complete template, like the one used in the get started tutorial.

All we need now is a basic index.php file in the web root:

<?php require_once 'vendor/autoload.php'; use BlueMvc\Core\Application; use BlueMvc\Core\Controller; use BlueMvc\Core\Request; use BlueMvc\Core\Response; use BlueMvc\Core\Route; class HelloController extends Controller { public function indexAction() { return 'Hello world'; } } $application = new Application(); $application->addRoute(new Route('', HelloController::class)); $request = new Request(); $response = new Response(); $application->run($request, $response);

This application will display the text "Hello World" in the browser when navigating to the root url (e.g. http://localhost/).

Let's find out how it works.

Initialization

require_once 'vendor/autoload.php'; use BlueMvc\Core\Application; use BlueMvc\Core\Controller; use BlueMvc\Core\Request; use BlueMvc\Core\Response; use BlueMvc\Core\Route;

Nothing special here.

Since bluemvc-core was installed with composer, we need to require the vendor/autoload.php file.

We also tell PHP that we want to use some classes from the BlueMvc/Core namespace.

The hello controller

class HelloController extends Controller { public function indexAction() { return 'Hello world'; } }

Here a controller is declared by extending the Controller class.

The indexAction, which will be invoked when the url ends with "/", is implemented to return the text "Hello world".

The application

$application = new Application();

Next, the Application is created.

The application is the main part of the framework and handles setup, routing, error handling and more.

Routing

$application->addRoute(new Route('', HelloController::class));

The application needs to know which controller to use for a specific request url.

This is done by adding a set of Route instances.

In this example, a Route is added that tells the application to use HelloController for all requests to the root-level directory in the url.

The request

$request = new Request();

The Request object contains information about the current request, e.g. url, query and form parameters, cookies and so on.

The response

$response = new Response();

The Response object in turn, will contain everything that is sent back to the client, such as the content, status code and more.

Run the application

$application->run($request, $response);

And finally, this is were the magic happens!

Very simplified, the flow is like this:

  1. The request is examined.
  2. The proper controller is created.
  3. A matching action method in the controller is invoked.
  4. A response is generated and returned to the client.

✎ Published in category core