Helium MVC: Routing, Controller and Views

Devin Dixon
Helium MVC
3 min readDec 11, 2018

--

After completing step 1 of getting your site up and running, our next step is is to get into the basics of how traffic is routed and eventually gives the end user a screen.

As a reference reminder, as you go through these tutorials, you are welcome to play with the example sites here:

https://github.com/Helium-MVC/Examples

Router

The first file we want to look at is how basics routes are created. In your site, go to the file: site/config/bootstrap/router.php

The basic setup of this file will consist of this:

PVRouter::addRouteRule(array(‘rule’=>’/:controller’));
PVRouter::addRouteRule(array(‘rule’=>’/:controller/:action’));
PVRouter::addRouteRule(array(‘rule’=>’/:controller/:action/:id’));

These rules added to the routes setss up how routes will interact with a controller.

The :controller Option

The above parameter /:controller sets up the basic rule that the first item in a URL represents the controller. Given that our example domain is localhost:

— localhost/index or just localhost/ will automatically go to the indexController
— localhost/users will automatically go the usersControllers
— localhost/posts will automatically go the usersControllers

To start, let's create a users and posts controller. Make the following:
site/controllers/usersController.php


<?php
include('baseController.php');
class usersController extends baseController {
public function index() {
echo ‘Users Controller’;
exit();
}
}

site1/controllers/postsController.php

<?phpinclude('baseController.php');
class postsController extends baseController {
public function index() {
echo ‘Posts Controller’;
exit();
}
}

Now if we navigate to the urls of /users or /posts, we will get an output!

  • localhost:8000/users
  • localhost:8000/posts

Action and Views

Next, we are going to create some actions with corresponding views. Referencing out router:

PVRouter::addRouteRule(array(‘rule’=>’/:controller/:action’));

This tells our application that the second option in the URL, /:action, is what is known as action. An action can be thought of what a page should do. For example:

  • localhost/users/register — The registration page for a user
  • localhost/posts/create — The page to create a post

Actions are designated as functions in our controllers. As an examples-

/users/register
will route to the below function inside the usersContoller:

public function register() {}

Let’s go ahead and add to our users and posts controllers.

site/controllers/usersController.php

<?phpinclude('baseController.php');
class usersController extends baseController {
public function index() {
echo ‘Users Controller’;
exit();
}
pubic function register() {
echo ‘Register’;
exit();
}
}

site/controllers/postsController.php

<?phpinclude('baseController.php');
class postsController extends baseController {
public function index() {
echo ‘Posts Controller’;
exit();
}

public function create() {
echo ‘Create a Post’;
exit();
}
}

By default, all default actions go to the index, and every controller is required to have an index function.

Tieing In the Views

For the last part of this section, we are going to tie in the views. If we go to site/views/, we already have 1 folder here, the index folder. The index folder ties directly to the indexController.php. For our usersController.php and postsController.php we are going to create the folders users and posts.

We have a total of 4 actions — index() and register() which are part of usersController, and index() and create(), which is part of the postsController. We are going to create responding templates of index.html.php , register.html.php and create.html.php, which corresponds to the actions. The action corresponds with the prefix of the .html.php.

Your folder structure should look like this at this point:

- view
— index
— —index.html.php
—users
— — index.html.php
— — register.html.php
— posts
— — index.html.php
— — create.html.php

After you have created the folders and files to match that setup, add some text to each of the [action].html.php files

Put Some Text Inside Of The View Files

Before moving forward, put some example text inside of the files we just created. It can be <h1>Hello Word</h1>, or any kind of html that you want. Our next step will be displaying that text.

Last Step — Remove The Exits From the Controllers

In our above examples, we had placed exit() functions in the controllers. We are going to remove those so our experience now freely flows into the views.

site/controllers/usersController.php

<?php
include('baseController.php');
class usersController extends baseController {
public function index() {

}
pubic function register() {

}
}

site/controllers/postsController.php

<?php
include('baseController.php');
class postsController extends baseController {
public function index() {

}

public function create() {

}
}

And we are done with this section!!! Go to those URLs and have fun checking them out!

  • localhost:8000/users
  • localhost:8000/posts
  • localhost:8000/users/register
  • localhost:8000/posts/create

We are on our way to constructing our site. Next we are going to learn a little bit about templates and passing data from controllers to views. https://medium.com/helium-mvc/basic-templating-and-passing-data-f98250e3dfca

--

--