edit

Controllers Aliases


Overview

Controller aliases are the heart of your application, as they determine how HTTP requests should be handled.

Tip

This tutorial assumes you’ve downloaded Skytells Framework and installed the framework in your development environment.

In this tutorials we will learn step by step how to create a Skytells Framework Controller Alias.

Difficulty

  • For Seniors : Easy
  • For Beginners : Easy

Definition of Controllers Alias

A Controller Alias is simply a class which extended from the class file that is named in a way that can be associated with a URI.

To give more functionality to your app controller.

Namespace

Alias Namespace

The Alias Namespace uses the parent controller's namespace.

Creating Controller Alias

All Controllers must be stored in this folder.

1
Application/Resources/Controllers/Aliases

Create a file inside the mentioned path to be like :

1
Application/Resources/Controllers/Alias/MyControllerAlias.php

Put with the following code in your controller.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyControllerAlias extends \MyController implements IController  {

  public function something() {
    // This is the default method for the controller.
    echo "Hello World from Alias";
  }
}

Important

Always make sure your controller extends the parent controller class

Now go to your app controller And put the following code on the __construct() function.

1
Application/Resources/Controllers/MyController.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  public function __construct() {
    parent::__construct();
    $this->AddAlias('MyControllerAlias', $this);
  }
}

Now you should be able to access the Alias controller functions by calling it like mentioned on the following example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  public function __construct() {
    parent::__construct();
    $this->AddAlias('MyControllerAlias', $this);
  }

  public function index() {
    return $this->MyControllerAlias->something();
  }
}

Now go to your controller by your browser

1
http://example.com/MyController

If you did it right, you should see: Hello World from Alias is printed on the screen.

Notice

Notice that the default method index() is automatically called upon accessing the URL by the parent controller.