Hello World

Maveric is an MVC framework, that means it breaks up development into three major parts:

  • Models, which are wrappers for data sources (like databases)
  • Views?, which contain presentation logic to format output, and
  • Controllers?, which hold the business logic.

In this example, we only really need Views and Controllers, since we won't be working with data sources. For more of an introduction, head over to GettingStarted?.

A new Maveric installation creates several folders. Everything has a place: controllers go in the controllers/ directory, views go in the views/ directory, etc.

To start our Hello World application, we need a new controller. Controllers are implemented by classes that end in Controller. In the controllers/ directory, create HelloworldController.php. You'll be defining a new class that extends ApplicationController.

Here's the contents of HelloworldController.php. Notice that we need to call parent::__construct().

<?php
// HelloworldController - Hello World!

class HelloworldController extends ApplicationController
{
  public function __construct() {
    parent::__construct();
  }
}
?>

Now we need to add an action. Actions are the things each controller can do. For example, a DocumentController might have view, create, edit, and delete actions. Actions are implemented by public methods of your controller class.

If no action is specified, Maveric will look for an action called index, the default, so let's create an index method.

<?php
// HelloworldController - Hello World!

class HelloworldController extends ApplicationController
{
  public function __construct() {
    parent::__construct();
  }

  public function index()
  {

  }
}
?>

If you've been editing an installation on http://localhost/maveric, then you should now be able to visit http://localhost/maveric/helloworld.

Oh no! That triggered an exception. There is no file called helloworld/index.php in the views/ directory.

The views/ directory is filled with subdirectories for each controller. For example, the views for a controller called UserController would be in views/user/. We need to create a directory called views/helloworld/.

The views themselves have the same name as their action. So, for HelloworldController->index(), we need a view called views/helloworld/index.php.

Here's the contents of views/helloworld/index.php:

<h1>Hello World!</h1>

Now let's try that URL again. http://localhost/maveric/helloworld.

So that accesses the index action, but what if we wanted to get to other actions? Well, the default URL routes are something like controller/action/id.format. (We don't care about id and format for now.) So you can try http://localhost/maveric/helloworld/index and you should see the same screen.

Now let's add a little personal touch to our HelloworldController.

<?php
// HelloworldController - Hello World!

class HelloworldController extends ApplicationController
{
  public function __construct() {
    parent::__construct();
  }

  public function index()
  {
    $this->name = ($_GET['name']) ? $_GET['name'] : 'World';
  }
}
?>

If you're not familiar with the ternary operator, it's  worth checking out.

Now let's go back to our helloworld/index.php view:

<h1>Hello <?php echo $this->name; ?></h1>

<form method="get">
<label for="name">What's your name? <input type="text" name="name" id="name" /></label><br />
<input type="submit" value="Say Hi!">
</form>

There we go, a nice, simple, Hello World application. Now you'll probably want to start exploring the other directories, like helpers/ and config/, and getting into Models.

For reference, here are the files we used/edited:

maveric/
  controllers/HelloworldController.php
  views/index.php