Controllers

Each controller to be used with the framework (i.e. render views, links to functions etc) must extend Controller class:

class MyAwesomeController extends Controller {
    
}

Each controller inherits a whole range of useful and needed features

linkTo

Should always be used when linking within the framework, because absolute links are needed by the routing and always checks against your custom routes.

//without custom routes
MyAwsomeController::linkTo("moreAwesomerFunction");
// returns http://localhost/PHP_PLAIN/index.php/MyAwsomeController/moreAwsomerFunction

From time to time, you need to pass some dynamic values to your controllers, e.g. an ID to display specific content. Therefore you can pass an array or lined up values to your controller function. These values occur as parameter in the same order as given to to linkTo function.

Users::linkTo("show", 3 );
// returns http://localhost/PHP_PLAIN/index.php/Users/show/3

//Users controller
public static function show($userId){
    // $userId = 3
}

Note: It is possible to line up as many parameters as you like.

For more informations, please visit the routing section


redirectTo

Is just the same usage like the linkTo function.

After a redirect no other code is interpret.

public static function login(){
    //a lot of validation
    if($success){
        MyAwesomeController::redirectTo("superSecretWebsite", 333);
    }
    
    //error - back to frontpage
    App::redirectTo("index");
    
    //unreachable code
    echo "yay";
}

render

The render method looks up the appropriate view and renders it on the index.php (in the yield())

Please find details in render views


renderPartial

The renderPartial method searches the associated view and renders it to the point where the call took place.

Please find details in render views


extendFromTemplate

With extendFromTemplate we can tell our render call to extend the corresponding view in the given template.

Please find details in the templates section


renderText

renderText does not render in the _yield() function, but prints the text with the correct Content-Type header (text / plain) set, and ends the script processing.

public static function debugMe(){
    self::renderText("plain text here");
    
    //unreachable code
    $var = "im completely useless";
}

renderJSON

renderJSON works similarly to renderText, but expects an array. The data will output as a json_encoded string with the Content-Type header is set to application / json. The script processing is suspended here.

public static function someJSONResponse(){
    self::renderJSON(array(
    	"ok" => true
    ));
}

always

The always function runs at the latest before each render call. So you can add stylesheets or scripts, extend all render calls from a template oe secure the whole controller with validations.

public static function always(){
    
    if($userValidationFailed){
        App::redirectTo("index");
    }
    
    self::addScript("prettify.js");
    self::addStylesheet("sunburst-theme.css");
}

Usually (in most other frameworks), this function is called as before(), as it simply is not true depending on the execution order, I was looking here for a new name.


addScript

There are always JavaScripts that can be used only on a single, or only a few pages. In order for this not to be loaded on every page request, you can load the required scripts in the corresponding controller function

public static function controllers(){
    self::addScript("prettify/prettify.js");
    self::addStylesheet("sunburst-theme.css");
    self::render();
}

addStylesheet

Has the same functionality as addScript, only for stylesheets

Also available in the above example


isActive

The isActive function tries to find out (based on the URL and the renderArgs) if the calling controller is the currently active.

As an optional parameter, the view (or function) cann be passed, so the \"active\" result is additionally limited to the view

A recent example: on the current page, the side menu is only displayed when you are in the Manual Controller.

if(Manual::isActive()){
    //render side menu
    Manual::sideMenu();
}    
//for active state in menu
if(Manual::isActive("controllers")) echo "style='color: blue;'";

keep & get

The keep function stores key - value pairs in a private session cookie, and kept as long until they brought back with the get() function.

public static function auth(){
    //authorization failed, store error in temp cookie
    self::keep("error", "failed to login for some reason");
}


public static function login(){
    //if nothing is set, get() returns false 
    //so every error disappers after a reload 
    //(see login example in the default download)
    self::render(array(
        "error" => self::get("error")
    ));
}

add & setRenderContent

With addRenderContent(key, val) you can add dynamic parts before the render() call, e.g. to set in a view a subtitle (or with setTitle the title tag in the index.php)

With getRenderContent(key) the content can be loaded in the view.

Difference to keep & get: renderContent is temporarily stored by the controller and issued with a render() call, and not stored in a cookie, so it can not be seen from outside, but also not valid for a request away.

    // App::index 
    App::addRenderContent("subtitle", "PLAIN_PHP is really simple, fast and i enjoy coding with it");
    
    // some view
    <?php echo Controller::getRenderContent("subtitle"); ?>

get & setTitle

setTitle is a shortcut for addRenderContent('title', your title) to fill the title tag.

    //in Manual controller 
    self::setTitle("PLAIN_PHP - Manual");
    
    //in the dafault index.php file
    <title><?php echo Controller::getTitle(); ?></title>