Working with MVC using Laravel 101

Laravel
starting with MVC and Laravel from Scratch

If you are afraid of coding or maybe MVC or maybe frameworks like Laravel, nette or *, don't worry, everyone is here to learn, someday you have to do it and trust me today is the day for you to learn MVC and Laravel ( checked your horoscope, your stars are shining bright )

Prerequisite:
  • A working system ( OS X/ windows / linux(preferred -> you'll get to know why) )
  • basic knowledge of php, html, css, javascript, firebase, angular js, mysql, node js, Java
  • An I.D.E. (hope you know, what we are dealing with) : PhpStorm (An advise not a suggestion)
  • Working internet connection
  • In a mood to do hands on
  • Ooh, you got lucky, you just need php and html ( told you your stars are bright)

 Let's begin now,
Step 1: Installing Laravel
  • Install Composer first, it'll help you to download all the dependencies which laravel or any other project require. You just need to command composer to do so.
  • Once done with the installation, lets create a simple project to create a login form
    • Open  Terminal and type the following command ( no need to add `$`)
      • $ cd Documents/Project 
      • $ composer create-project laravel/laravel login
        • laravel/laravel: It is the package name
        • login: It is project name ( you can change it to whatever you want it to be )
      • This will do all what is required for the setup part
Step 2: Lets start some coding here

  • Go to the routes file (routes.php), you'll find that in app->Http directory
          delete everything there and type this code:

    <?php
                  Route::get( '/' , 'PagesController@index');
                  // what this is doing is redirecting you to the index method of PagesController which we'll                  //  create later
          ?>
  • In the Controller directory in app->Http->Controller ( there are chances you might find a PagesController.php), but we'll create from scratch so you don't have to worry

    -> Create a PagesController.php and paste the code below:

    <?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use Request;
    use App\Http\Requests; class PagesController extends Controller{ /** * @return string */ public function index() { return view('pages.index'); // This will return the view named 'index' which is placed in the views directory // So create a view named 'index.blade.php' you can google what blade is, but we can skip it for now } } ?>
      
    • I guess now you can implement the knowledge gained from previous blog, with the help of this controller we are redirecting to the view
  • Now create the index.blade.php in the views directory
    • Put the html form code in it, I think you can do it
    • label the forms etc etc and action = "/home" method= "POST/GET"
      • lets say we have 2 attributes `name` & `password`
    • Our view v1 is created
  • Now lets move to the model but before that lets route this using ______.php (fill in the blank)
    • Add the following line of code to it
      //If you used post method, for get method( Replace post with get, nothing else!! )

      Route::post( '
      /home' , 'PagesController@show_login');

      //This will call the 'show_login' method from the PagesController
  • Back to PagesController.php
    • Add the business logic here:
       
      public function show_login(){
      
      //Lets hard-code the authentication


      $username = Request::get('name');
      $pass = Request::get('password');

      if($username == 'Blogger' && $pass == 'Gmail@123')
      return view('welcome');
      else
      return view('/');
      }

  • Create a new welcome page and display whatever you want to display


    Congratulations, you are done with your first MVC based web application using Laravel by just typing 15 Lines of Code, trust me ( I have counted them (excluding lines containing parenthesis) ) , so get your hands dirty with this, rush back and add laravel & MVC based web application development to your Resume/CV.

    You can thank me by liking/ sharing the post!

    Next Blog on: Swift vs Objective C + Beacons + Cocoa pods ( I don't have a mac at home so                             cannot teach swift and iOS mobile application development, I'll try to cover                               some aspects of it but it'll more of theory but it'll be interesting.

    Till then,
    Adios Lactores
    Happy Blogging

Comments

Popular posts from this blog

A Brief about RDMA, RoCE v1 vs RoCE v2 vs iWARP

Cryptocurrency & Blockchain

Repairing your Ubuntu or Linux Dist. manually