Sample Application - Furbook
Laravel Version: 5
Date: June 2016
Laravel Sample Application: Furbook - show cat profiles
Application Setup
setup Homestead VM (vagrant)
create new Site called FurBook (as vagrant user)
cd /home/vagrant/code
mkdir furbook.app
cd furbook.app
composer create-project laravel/laravel . --prefer-dist
This will create new skeletal structure for the project
If you have multiple Sites/Projects on a single Homestead, you can let Nginx manage all of them and route requests according to the supplied URL (even though they all share the same IP)
new site name: furbook.app
cd /vagrant/scripts
serve-laravel.sh furbook.app /home/vagrant/code/furbook.app/public
on your host machine (not Homestead VM), edit /etc/hosts to allow multiple sites to be accessed, for example if you have 2 sites SuperSite.app and Furbook.app, and both are on Homestead with an IP of 192.168.56.10, have /etc/hosts point to the same IP
cat /etc/hosts
## LARAVEL
192.168.56.10 supersite.app
192.168.56.10 furbook.app
restart Nginx and test changes by going to each Site URL in the browser
supersite.app and furbook.app, both should show different index.html
when testing, use Site names ending with .app not .com, since you will be hitting actual live sites with .com
test new site by going to furbook.app in your host's browser, if you can see a generic Laravel index page, its up and running
give your app an official name
homestead$ php artisan app:name Furbook
Routes
This application will show cat profiles,
users going to http://furbook.app/ will be redirected using a Route, to create new profiles goto /furbook.app/create (this is Route)
create new Route in /home/vagrant/code/furbook.app/app/Http
open the routes.php, add new routes (http pointers)
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('all cats');
});
Route::get('cats/{id}', function($id) {
return sprintf('Cat #%s', $id);
});
Test the routes by going to http://furbook.app/cats/123
it should return: Cat #123
to restrict it to only take in numbers add a 'where'
Route::get('cats/{id}', function($id) {
return sprintf('Cat #%d', $id);
})->where('id', '[0-9]+');
Redirections
if you need to redirect visitors to a certain page, just add a redirect function
Route::get('/', function() {
return redirect('cats');
});
anyone going to http://furbook.app will be redirected to /cats directory
404 and Error Pages
to create custom error pages according to Error code, create page for each error code in
/home/vagrant/code/furbook.app/resources/views/errors
404.blade.php
403.blade.php
503.blade.php
Views
Views are presentation pages on the Presentation layer.
To create an 'about' page, go to
/home/vagrant/code/furbook.app/resources/views/
create about.php
<h2>about this site</h2>
there are over <?php echo $number_of_cats; ?> cats on this site
create a new Route to this page,
vi /home/vagrant/code/furbook.app/app/Http/routes.php
Route::get('about', function () { return view('about')->with('number_of_cats',9000); });
test in in browser, http://furbook.app/about
Configure DB and create Database Tables
edit /home/vagrant/code/furbook.app/.env
set DB_DATABASE = furbook
create new Cat model, this will be used to manipulate data for cats
create file /home/vagrant/code/furbook.app/app/Cat.php
<?php namespace Furbook;
use Illuminate\Database\Eloquent\Model;
class Cat extends Model {
protected $fillable = ['name','date_of_birth','breed_id'];
public function breed() {
return $this->belongsTo('Furbook\Breed');
}
}
create Breed.php to link Cats to Breeds, in /home/vagrant/code/furbook.app/models/Breed.php
class Breed extends Eloquent {
public $timestamps = false;
public function cats(){
return $this->hasMany('Cat');
}
}
run this to create DB Migration, this is a PHP script to create DB table that will generate this PHP code, function up will create the table, function down will drop the table
> cd /home/vagrant/code/furbook.app/
> php artisan make:migration create_breeds_table --create=breeds
cat /home/vagrant/code/furbook.app/database/migrations/NAME_OF_GENERATED_MIGRATION
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBreedsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('breeds', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('breeds');
}
}
Run the process to create Cats migration,
> cd /home/vagrant/code/furbook.app/
> php artisan make:migration create_breeds_table --create=breeds
edit the generated PHP file to include Name and Date of Birth fields, also add the breed_id foreign key
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCatsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cats', function (Blueprint $table) {
$table->increments('id'); #auto-incremental primary key
$table->string('name');
$table->date('date_of_birth');
$table->integer('breed_id')->unsigned()->nullable(); #column can have NULL values
$table->foreign('breed_id')->references('id')->on('breeds'); #foreign key to Breeds table
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('cats');
}
}
Eloquent
Eloquent is a syntax used to talk to the DB. You talk to the DB tables through Eloquent Models
Tinker
use Tinker to play around with Laravel elements
cd /appname/
php artisan tinker