Routing & Controllers

Routing is the process of resolving a HTTP request into your application to an action which produces a response and potentially performs logic. Routing and routes within the framework are primarily concerned with two pieces of HTTP information for each request, the request type; GET, POST, PUT, or DELETE and the request URI, or the portion of the URL that comes after your domain name and TLD (ie .com).

Controllers are responsible for producing the response and performing the logic for a specific route, ie for a specific request type and request URI. Controllers can take the form of a Closure or Anonymous function or a class/method combination. When classes are used as controllers they can extend the \Disco\classes\Controller class to gain some added functionality which aides in returning responses. Make sure you read the API to see what the controller class can do for you out of the box. Of course you can create your own base controller that extends the Disco controller which you then extend your controllers with to provide further functionality.

Example Controller #

Below is a stubbed example controller for handling images, most of the functionality is hidden away in a imagined `ImageService` service which isn't listed here to keep the example concise. This controller also uses the Image view (learn about views) which isn't listed here either, to keep things simple.

<?php
namespace App\controller;

class Image extends \Disco\classes\Controller {


    /**
    * This controller uses the Images view, so let set it up
    */
    public function __construct(){
        \App::make('View', 'App\view\Images');
    }//__construct



    /**
    * List all the uploaded images
    */
    public function getIndex()

        // get the images that have been uplaoded via the ImageService
        $images = enact()->with('ImageService')->getAllImages();

        \View::title('Images uploaded by our users');
        \View::desc('The complete listing of images uploaded by our users.');

        return $this->template('image/index.html', $images);

    }//getIndex



    /**
    * Present the image upload form to the user.
    */
    public function getUploadImage(){

        \View::title('Upload Image');

        return $this->template('image/upload.html');

        // returning a template is the same as calling:
        // \Template::with('image/upload.html');
        // or 
        // \View::html( \Template::render('image/upload.html') );

    }//getUploadImage



    /**
    * Upload an image and return an ajax response with errors and successes.
    */
    public function postUploadImage(){

        $response = enact()->with('ImageService')->upload($_FILES);

        return $this->json($response);

    }//postUploadImage



    /**
    * Delete an uploaded image.
    */
    public function deleteImage($id){
        return $this->json(enact()->with('ImageService')->delete($id));
    }//deleteImage



    /**
    * Serve the image as a download to the user.
    */
    public function getDownloadImage($id){
        return $this->download(enact()->with('ImageService')->getAbsolutePath($id));
    }//getDownloadImage



    /**
    * Servce the image as a resource
    */
    public function getImage($id){
        return $this->file(enact()->with('ImageService')->getAbsolutePath($id));
    }//getImage



}//Image

Routes To The Image Controller #

Now we will set up the routes to the controller.

Router::get('/images/', 'App\controller\Image@getIndex');


Router::get('/images/upload', 'App\controller\Image@getUploadImage');


Router::post('/images/', 'App\controller\Image@postUploadImage');


Router::delete('/images/{id}', 'App\controller\Image@deleteImage')
    ->where('id', 'integer');


Router::get('/images/{id}/download', 'App\controller\Image@getDownload')
    ->where('id', 'integer');


Router::get('/images/{id}/view', 'App\controller\Image@getImage')
    ->where('id', 'integer');

Make sure you check out the Router Service to see all the awesome things you can do with your routers.