Results 1 to 2 of 2

Thread: Laravel Controllers

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Laravel Controllers

    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', '[email protected]');
    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:
    [email protected]:~/www/sherly.dev $ cat app/views/login.blade.php 
    <h1>Login</h1>
    
    HTML Code for user login page goes here.
    [email protected]:~/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
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    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', '[email protected]');

    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:
    <h1>Login</h1>
    
    HTML Code for user login page goes here.
    Now if you go to login URL, you get following page


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •