Controller
Instead of writing logic in the routes.php, you can separate and organize your application logic into a controller. Controller class is by default located in app/controllers directory, but you can move it to another directory as long as it registered in composer.json
<?php
//file : app/controllers/HomeController.php
Class HomeController extends BaseController{
/**
* Display welcome page
*/
public function welcome(){
$this->data['title'] = 'Welcome SlimStarter!';
View::display('welcome.twig', $this->data);
}
}
And now we can add route to this controller in app/routes.php
<?php
//file: app/routes.php
Route::get('/', 'HomeController:welcome');RESTful Controller
SlimStarter allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions using the Route::controller method:
Route::controller('book', 'BookController');
this will map every access to /book to be handled by BookController
class BookController extends BaseController {
/**
* access /book via get method
*/
public function getIndex()
{
//
}
/**
* access /book/store via post method
*/
public function postStore()
{
//
}
/**
* access /book/update via put method
*/
public function putUpdate()
{
//
}
}Resource Controller
Resource controller make it easier to build RESTful controllers around resources. For example, you may wish to create a controller that manages “books” stored by your application.
Route::resource('book', 'BookController');
this will map several route to BookController as listed below
Route::get('/book', 'BookController:index'); // GET /book
Route::post('/book', 'BookController:store'); // POST /book
Route::get('/book/create', 'BookController:create'); // Create form of /book/create
Route::get('/book/:id', 'BookController:show'); // GET /book/:id
Route::get('/book/:id/edit', 'BookController:edit'); // GET /book/:id/edit
Route::put('/book/:id', 'BookController:update'); // PUT /book/:id
Route::delete('/book/:id', 'BookController:destroy'); // DELETE /book/:id
Leave A Comment