Controllers - adampatterson/Dingo-Framework GitHub Wiki

What Are They?

Controllers in Dingo display pages, make database queries, and pass data to Views. Controllers provide the core functionality to Dingo applications.

Basic Usage

Let’s say you want to make a page about yourself. We are going to create a new controller just for that. Create a new PHP file at application/controllers/about.php. This file will be our new controller.

When the web browser requests the page index.php/about Dingo will load the about.php controller and run the controller-\>index() function. If the controller or the function is missing then Dingo will display a 404 page not found error.

Now in our new controller create the controller class and the index() function.

<?php

class about_controller
{
  public function index()
  {
echo "This is about me!";
  }
}

Now if you visit index.php/about in your web browser you should see “This is about me!”.

Add Onto It

Let’s say you have a good friend Bob. He sees how amazingly awesome your website is and wants a page about himself on it. Because of the way Dingo’s controllers work this should be easy as pie. All you have to do is add the function bob() onto your controller class.

<?php

class about_controller
{
  public function index()
  {
    echo "This is about me!";
  }

  public function bob()
  {
	echo "This is Bob's page";
  }
}

Now when you visit index.php/about/bob Dingo will run the bob() function in the about controller, which would display “This is Bob’s page”.

But wait! How come when you visit index.php/about it runs the index() function? This happens because by default if you just visit the controller Dingo automatically runs the index() function. So, if you visit index.php/amazing it will run the index() function in the amazing.php controller.

Arguments

URLs in Dingo can be infinitely long. What that means is that after the controller and function portions on the URL all other portions are just arguments for the controller function. Edit bob() so it looks like this:

public function bob($message)
{
  echo "You said: $message";
}

Now if you visit index.php/about/bob/cool it will display “You said: cool”.

Now there is one problem with this. If you where to visit index.php/about/bob then you would get a 404 error! This is because you can’t just run bob(), it has a required argument! If you want people to still be able to visit just plain old bob() then you need to make your argument optional:

public function bob($message = 'nothing at all')
{
  echo "You said: $message";
}

Now visiting index.php/about/bob will produce “You said: nothing at all”, but visiting index.php/about/bob/cool will still display “You said: cool”.

At this point you should try experimenting around with adding additional controllers, functions, and arguments and see what happens.

Parent Controllers

In Dingo, it is possible to make controller classes extend from other controller classes. For example you may have a controller that looks like this:

<?php

class base_controller
{
  public function message()
  {
    echo "Hi!";
  }
}

Then, you could have another controller that extends from your parent controller so it can access the message() method.

<?php

load::parent_controller('base');

class another_controller extends base_controller
{
  public function index()
  {
    $this->message();
  }
}

Parent controllers may have their own parent controllers as well.