Skip to content

8. Controllers

DA edited this page Apr 4, 2019 · 25 revisions

Getting Started with Controllers

A Controller is a key entity in an MVC architecture. It is the layer that is responsible for interfacing with both the Model and the View. It takes its input from a View, interfaces with the Model to either get information or add to the information, it calls upon any other logic that needs to be applied to the information and finally sends it back to the View to be displayed.

Most of the concepts that we covered thus far - Routing, Request, Models, Relationships, Validation, Views - come into play together in the context of a Controller.

Creating a Controller

  • All the controllers that you create should be in the /application/controllers folder. In this folder, you can organize the controllers to your convenience. If you prefer to have your controllers in a different folder, you can always do that for as long as it is within the /application folder.

  • There is no formal naming convention for a controller. We do recommend that you remain consistent.

Learn more about naming conventions here

  • All the controllers that you create should extend the Controller class
class BookController extends Controller {
    // your code goes here
}
The controllers that you create need to have at the very least, an `index` method. The `index` method gets called by default when no method is specified in the `Route`.
class BookController extends Controller {
    // index method
    public function index()
    {
       // your code goes here
    }
}
  • Controllers usually cover all the actions that can be taken on the Model that it controls. It is therefore common to have several other methods within a controller.
class BookController extends Controller {
    // index method
    public function index()
    {
       // your code goes here
    }
    public function read()
    {
       // your code goes here

    }
}

Routing to the Controller

In the Routing section, we covered the basics of how to route a request to a Controller or to a specific method within the controller. We will summarize them herewith.

The following route will redirect all calls to http://your-app/books to the index method in the BookController:

Route::get('books', 'BookController');

The following route will redirect all calls to http://your-app/books to the read method in BookController:

Route::get('books', 'BookController@read');

Controller Context

Frameworks are meant to help organize and structure code. Some frameworks impose that a Controller be tied to a Model and imposes a naming convention that forces developers to have a one controller for one model.

Our framework provides the flexibility to create a controller that is independent of a model and it allows changing its context at any point using the associate method that takes a Model and a database table name arguments and returns a Controller:

public function associate(Model $model, String $tablename);

Let's consider the books example again. Assume you have a Book and a Publisher models in an online library system. In a model-constrained framework, if you need to allow the users to make changes to the Publisher model, you will need to have a dedicated PublisherController.

In this framework, you could change the context in methods or even within the same method:

class MyController extends Controller {

    public function readBook()
    {
       // associate the controller with the Book model and the books table
       $this->associate('Book', 'books'); 
       
       // all controller methods executed below will be in the context of Book
     }
     public function addPublisher()
     {
       // associate the controller with the Book model and the books table
       $this->associate('Publisher', 'publishers'); 

       // all controller methods executed below will be in the context of Publisher
 
     }
}

Controller Methods

From the moment a Controller is a created as previously described, powerful built-in methods are provided out of the box.

public function associate(Model $modelname,  String $table); // allows the model to switch context

public function all(); // returns all the entries of a model

public function find($id = null); // returns a specific entry. When id is not provided it is fetched from the request URI

public function save($inputs, $id = null); // saves an input Array or Model to the database. When an id is provided it would update

public function delete($id = null); // delete a specific entry. When id is not provided it is fetched from the requested URI

public function search(String $scope = null, String $keywords = null); // returns all entries in a Model with an attribute that matches the keyword search

public function table(); // returns the database table that the model is associated to at any point in time

public function check($data, $rules = array(), $filters = array()); // calls the GUMP validator for validation and /or filtering

public function validate($data, $rules); // calls the GUMP validation method

public function filter($data, $filters); // calls the GUMP filtering method

public function view($viewName, $viewData = array()); // returns a view that renders a Pug template with the data provided



Next Section: Learn about Helpers, Forms & REST API

Clone this wiki locally