My Main Blog

Saturday 22 November 2014

How to create controller in Laravel



There are two methods to create controller in Laravel.
A) By command prompt using "artisan controller:make" :
B) By manually creating controller.
To configure Laravel look here 

A) By command prompt using "artisan controller:make" :
I am assuming you are using wamp 2.5.
  1. Go to in the "c:\wamp\www\" folder
  2. Open command window and cd in "c:\wamp\www\"
    type "composer create-project laravel/laravel TestPro1" in command window; where composer is the exe file name "create-project" is the keywors, "laravel/laravel" is the keyword, "TestPro1" is the project name.
    This will create a folder "TestPro1" with supporting directories and files.
  3. Rename "server.php" to "index.php"
  4. Open command prompt in "d:\wamp\www\TestPro1" and type the                                    "php artisan controller:make MyController".   *Keep in mind that you must in the directory "TestPro1" when you issue the command in command window                            "php artisan controller:make MyController" and there must be a file "artisan", "composer.json", "composer.lock".
  5. This will create a file "MyController.php" in the "TestPro1\app\controllers". 
  6.  replace it's index() method as below
  7. public function index()
        {
             return "
    This is my web page in Laravel";
        }
  8. and register it by "Route::resource('welcome','WelcomeController');" in "routes.php" under "D:\wamp\www\TestPro1\app" folder.
  9. now you can see page content y http://localhost:/TestPro1/
B) By manually creating controller. 
Goto "d:\wamp\www\TestPro1\app\controllers" folder. create a file "MyController.php". and paste listed below content.

<?php
class MyController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        return "This is my web page in Laravel";
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
 
public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

 /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }

 /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

and register it by "Route::resource('welcome','WelcomeController');" in "routes.php" under "D:\wamp\www\TestPro1\app" folder.
now you can see page content y http://localhost:/TestPro1/

1 comment: