Laravel
Prerequsites
Some extensions that are required include:
# yum -y install php-dom php-mbstring php-zip
Creating a New Project
Create a new Laravel project using Composer. Specify the directory to contain the new Laravel project.
composer create-project laravel/laravel --prefer-dist $PROJECT_DIRECTORY
Permissions
Laravel requires directories under the storage
directory to be writable. If your web server is using mod_php
where PHP code is executed as the web server, you must chmod
the directories to be world readable.
Configuration
Configurations are defined inside the .env
file located in the root directory of your laravel project. Variables defined in this file override any that are defined in the application configuration files and is the recommended way of setting values.
Always Edit the .env
Environment File
Always make changes to the .env
environment file instead of changing the config file directly.
Revisioning software such as Git/SVN can be made to ignore certain files such as the .env file. In fact, a new Laravel project will have a .gitignore
file set by default to exclude .env
from being included. This is useful when you're developing a project on your personal dev machine which may have different settings than other members in your team.
.env
file, you do not risk accidentally exposing your private information.
Managing the Project using Artisan
Laravel comes with artisan, a PHP helper script that does common tasks while developing the Laravel project. Artisan is located at the root of the project directory.
Create an Alias for Artisan
You may want to create an alias for artisan by runningalias artisan='php artisan'
To list all the available commands that Artisan can take, invoke artisan.
Database
Laravel supports various databases through its ORM. You can set the default database connection and the database properties by editing the .env
Environment file.
Migration
Migration is a way to define database schemas and schema changes using PHP. When used with a version control system, it is trivial to replicate, update, or revert database schemas on your local system.
When creating models using `artisan`, `artisan` will automatically create a migration for the table referenced by the new model. After defining the schema in the migration file, you can apply the schema by executing:
artisan migrate
You can reset (drop + re-migrate) using:
artisan migrate:reset
Database Seeding
The `DatabaseSeeder.php` file defines how the database is to be seeded, say after an initial migration. It is possible to import data from an existing database to your development database by executing raw SQL queries like so:
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('WordsTableSeeder');
}
}
class WordsTableSeeder extends Seeder {
public function run() {
DB::table('words')->delete();
DB::select(DB::raw("INSERT INTO chinese_laravel.words
(word, pronunciation, frequency)
SELECT word, pronunciation, frequency FROM chinese.word"));
}
}
Application
For most web applications, the business logic code goes under the app/Http
directory.
Routing
HTTP requests will go through the app/Http/routes.php
file to determine the proper controller the request must go through.
You can list all routes that the Laravel project has defined by running artisan route:list
Controllers, requests, and middleware are placed in their respective directory under app/Http
.
Models
Models are placed directly in the app
directory and under the `App` namespace. When working with Models, reference them directly by their namespace or pull in the namespace.
Views
Laravel makes use of views through its Blade templating engine. Use the View facade to pull in views. Read more about Blade at http://laravel.com/docs/5.0/templates.
Quick Overview
Define a new section using:
@section('name') @stop
Section can also be used to define a key/value pair.
@section('key', 'value')
where key
can be used in a parent template (from extending) in
@yield('key')
Variables can be recalled using Template:$variable
Forms
To use the Illuminate HTML package, pull in the `illuminate/html` dependency using Composer.
composer require illuminate/html
Add the Illuminate\Html\HtmlServiceProvider to the service provider list:
Illuminate\Html\HtmlServiceProvider::class,
Then add the two class aliases:
'Form' => 'Illuminate\Html\FormFacade', 'Html' => 'Illuminate\Html\HtmlFacade',
In the blade templates, you can now use:
{!! Form::open() !!} {!! Form::label('name', 'Name:') !!} {!! Form::text('name', 'default', ['class' => 'blah'] ) !!} {!! Form::close() !!}
Quick Guide on Composer
Composer is a PHP package manager which automatically handles package dependencies. Composer comes as a PHP archive (phar) file that can be executed using a recent version of PHP with phar support.
Installation
Install composer on the shell by running the code below. Optionally specify the install directory.
$ wget -O - https://getcomposer.org/installer | php -- --install-dir=$HOME/tmp/
Normally, to invoke Composer, one would need to run
$ php $HOME/tmp/composer.phar
To make this easier, create an alias instead by putting the following in your .bashrc
file or by running it:
$ alias composer='php $HOME/tmp/composer.phar'
Updating
Use composer to update a project's dependencies by running composer update
on the project root directory.
Miscellanaeous
Miscellaneous Tools
When working with Less or Sass, you may wish to use Laravel Elixir which utilizes gulp. Install gulp by running as root:
# npm install --global gulp
You may also want to install bower, a package manager for web stuff
# npm install --global bower
Then run on the root project directory:
# npm install
or extra plugins with:
# npm install gulp-minify-css --save-dev
Bootstrap
# echo '{"directory": "vendor/bower_components"}' > .bowerrc
# bower search bootstrap-sass
# bower install bootstrap-sass-official --save
The bower files will now be in vendor/bower_components/
Troubleshooting
You can see what packages and dependencies were installed by running:
$ composer show -i
All HTTP routes can be listed using artisan
:
$ artisan route:list