Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 2.09 KB

controller.md

File metadata and controls

60 lines (44 loc) · 2.09 KB

Using Controllers

Controllers are simply classes that serve as bridges between Models and the View part of your application. Don't think too much of controllers, they're nothing but a class.

In this section, we'll be looking at how to handle a route with a controller. So let's make an example controller: remember it's just a php class

<?php
class HomeController {
	public function index() {
		echo "This is the index function";
	}
}

To handle a route using this controller, we'll have to import the controller, and then define a simple route

$leaf = new Leaf\App;

require "HomeController.php";

// we leave out the second parameter for now
$leaf->get("/home");

When using controllers, instead of defining a closure or function as the second parameter of your route, you rather pass in a string of the controller's class name and the function you want to use. In this case, "HomeController@index", so remember, it's Class@Method

$leaf = new Leaf\App;

require "HomeController.php";

$leaf->get("/home", "HomeController@index");

Controller Namespaces

In case you're using an auto loader or using leaf in another framework and you have your controllers in another directory, you can do sommething like this

$leaf->get('/(\d+)', '\App\Controllers\User@showProfile');

But this gets tedious if you have a lot of routes. So Leaf allows you to set a "general" namespace, you can set the default namespace to use on your router instance via setNamespace()

$leaf->setNamespace('\App\Controllers');

$leaf->get('/users/(\d+)', 'User@showProfile');
$leaf->get('/cars/(\d+)', 'Car@showProfile');


Request Response Session Environment Using a database


Built with ❤ by Mychi Darko