Laravel: Difference between revisions

From Network Security Wiki
Content added Content deleted
Line 59: Line 59:
==ROUTES==
==ROUTES==


cd routes/
cd routes/
sudo nano web.php
sudo nano web.php
<pre>

Route::get('contact', function () {
Route::get('contact', function () {
return 'Hello from Aman';
return 'Hello from Aman';
});
});



Route::get('contact/{category}', function ($category) {
Route::get('contact/{category}', function ($category) {
return 'Hello from '.$category. ' Aman';
return 'Hello from '.$category. ' Aman';
});
});
</pre>


Check resulting pages:

http://192.168.1.35/hello/public/contact/
access page:
http://192.168.1.35/hello/public/contact/
http://192.168.1.35/hello/public/contact/Test
http://192.168.1.35/hello/public/contact/Test



==VIEWS==
==VIEWS==

Revision as of 09:02, 20 September 2017

Installation

sudo apt install composer

Creating a project named 'code':

composer create-project --prefer-dist laravel/laravel code
cp .env.example .env
php artisan key:generate
sudo chmod -R 777 storage/

Tesing output:

php artisan serve --host 0.0.0.0 --port 8082

Else

sudo nano /etc/apache2/sites-available/laravel.conf
<VirtualHost *:8082>
    ServerName localhost

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/code/public

    <Directory /var/www/html/code/public>
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
sudo nano /etc/apache2/ports.conf
Listen 8082
sudo a2ensite laravel.conf
sudo service apache2 restart

Auth

Enable Authentication

php artisan make:auth


DB access

.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
create database homestead;
grant all privileges on homestead.* to homestead@localhost identified by 'secret';

Site Creation

ROUTES

cd routes/
sudo nano web.php
Route::get('contact', function () {
    return 'Hello from Aman';
});

Route::get('contact/{category}', function ($category) {
    return 'Hello from '.$category. ' Aman';
});

Check resulting pages:

http://192.168.1.35/hello/public/contact/
http://192.168.1.35/hello/public/contact/Test

VIEWS

cd ../resources/views/ aman@ubuntu:/var/www/html/hello/resources/views$ ls welcome.blade.php aman@ubuntu:/var/www/html/hello/resources/views$ sudo touch master.blade.php

<!DOCTYPE html> <html lang="en"> <head>

<meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE-edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>HELLO</title>

</head> <body>

<nav class="navbar navbar-inverse">

</nav> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" >

@yield('content')


</body> </html>


cd ../../routes/ aman@ubuntu:/var/www/html/hello/routes$ aman@ubuntu:/var/www/html/hello/routes$ ls api.php channels.php console.php web.php aman@ubuntu:/var/www/html/hello/routes$ aman@ubuntu:/var/www/html/hello/routes$ sudo nano web.php


Route::get('/home', function () {

   return view('home');

});

Route::get('/view', function () {

   return view('view');

});

Route::get('/new', function () {

   return view('new');

});


Route::get('/edit', function () {

   return view('edit');

});



cd ../resources/views/ aman@ubuntu:/var/www/html/hello/resources/views$ sudo nano master.blade.php welcome.blade.php aman@ubuntu:/var/www/html/hello/resources/views$ sudo touch view.blade.php aman@ubuntu:/var/www/html/hello/resources/views$ aman@ubuntu:/var/www/html/hello/resources/views$ sudo nano view.blade.php

@extends('master')

@section('content')

VIEW all content goes here

@endsection


sudo cp view.blade.php home.blade.php

@extends('master')

@section('content')

HOME all content goes here

@endsection

sudo cp view.blade.php new.blade.php ~ sudo cp view.blade.php edit.blade.php ~

CONTROLLERS

Routes page can have URLS but this is for small Apps only for Industrial ones, use Controllers

sudo php artisan make:controller RecordController


cd app/Http/Controllers/ sudo nano RecordController.php

class RecordController extends Controller {

 public function showAll()
       {
               return view('view');
       }
 public function showNew()
       {
               return view('new');
       }
 public function showEdit()
       {
               return view('edit');
       }

}


cd ../../routes/ aman@ubuntu:/var/www/html/hello/routes$ sudo nano web.php


Route::get('/view', 'RecordController@showAll');

Route::get('/new', 'RecordController@showNew');

Route::get('/edit', 'RecordController@showEdit');


DATABASE

Create new DB: hello

sudo nano .env

~add DB credentials


Create a DB table using Artisan:

sudo php artisan make:migration create_records_table

Created Migration: 2017_09_19_190352_create_records_table


cd database/migrations/ aman@ubuntu:/var/www/html/hello/database/migrations$ ls 2017_09_19_190352_create_records_table.php

Easy to create tables with Migrations Easy to manage them in large environments,


sudo nano 2017_09_19_190352_create_records_table.php

   public function up()
   {
       Schema::create('records', function (Blueprint $table) {
           $table->increments('id');
           $table->string('country');
           $table->string('city');
           $table->string('date');
           $table->string('no_of_aliens');
           $table->string('color_of_aliens');
           $table->timestamps();
       });
   }
   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists('records');
   }


sudo php artisan migrate


ELOQUENT

sudo php artisan make:model Record

cd app/


sudo nano Record.php

class Record extends Model {

   protected $table='records';
   protected $fillable=['id','country','city','date','no_of_aliens','color_of_aliens'];

}


Feed test data using Seeding:

create seed file:

sudo php artisan make:seeder RecordsTableSeeder


cd database/seeds/ aman@ubuntu:/var/www/html/hello/database/seeds$ sudo nano RecordsTableSeeder.php

use App\Record; //to import records.php file

class RecordsTableSeeder extends Seeder {

 public function run()
   {
      for ($i=0; $i<20; $i++)
       {
          Record::create
           (
               [
                  'country' => "country$i",
                  'city' => "city$i",
                  'date' => "date$i",
                  'no_of_aliens' => "no_of_aliens$i",
                  'color_of_aliens' => "color_of_aliens$i"
               ]
           );
       }
   }

}



sudo nano DatabaseSeeder.php


use App\Record;

class DatabaseSeeder extends Seeder {

   public function run()
   {
       $this->call('RecordsTableSeeder');
   }

}


aman@ubuntu:/var/www/html/hello$ sudo php artisan db:seed

Seeding: RecordsTableSeeder