Laravel Controllers

So far, we created routes and views.

Views are HTML, display logic.

Our routes just returned View. But for complex applications, you will need to do more than just returning a view.

So instead of returning view, routes pass request to Controller, that will do the request processing and return appropriate View.


Creating Controller

Controllers are stored in folder

Code:
app/controllers
By default, there are 2 files in controllers folder.



Lets create a new Controller.

Code:
UserController.php
With following content.

Code:
<?php

class UserController extends BaseController {

    public function login()
    {
        return View::make('login');
    }

}
Routing to Controller

Lets say we need following Link routed to login function in UserController

Code:
http://localhost:8000/login
For this, edit

Code:
app/routes.php
Add following


Code:
Route::get('/login', 'UserController@login');
Now, if you go to url

http://localhost:8000/login

You get view not found error as in previous tutorial. So create view file in app/view folder.

Code:
boby@fwhlin:~/www/sherly.dev $ cat app/views/login.blade.php 
<h1>Login</h1>

HTML Code for user login page goes here.
boby@fwhlin:~/www/sherly.dev $
Now if you go to login URL, you get following page



To understand more about Laravel Controllers, read

http://laravel.com/docs/4.2/controllers