Laravel: Difference between revisions

From Leo's Notes
This page was last edited on 9 October 2015, at 01:09.
reformat. More information on laravel and composer.
Line 1: Line 1:


== Installation ==
== Creating a New Project ==
Download composer and create an alias so that `composer` launches composer:
Create a new Laravel project using [[Composer]]. Specify the directory to contain the new Laravel project.
wget -O - https://getcomposer.org/installer | php -- --install-dir=$HOME/tmp/
{{Highlight|lang=bash|code=
alias composer='php $HOME/tmp/composer.phar'
composer create-project laravel/laravel --prefer-dist $PROJECT_DIRECTORY
}}


Set up the laravel project using composer:
=== Permissions ===
composer create-project laravel/laravel projectname --prefer-dist
Laravel requires directories under the {{code|storage}} directory to be writable. If your web server is using {{code|mod_php}} where PHP code is executed as the web server, you must {{code|chmod}} the directories to be world readable.


There are many useful commands that comes with laravel's artisan command line PHP script located in the root of the project directory. You may want to create an alias for artisan:
=== Configuration ===
alias artisan='php artisan'
Configurations are defined inside the {{code|.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.


The initial laravel project will come with scaffolding project files which you may want to remove by running:
{{Info|Always Edit the {{code|.env}} Environment File|Always make changes to the {{code|.env}} environment file instead of changing the config file directly.
artisan fresh


Ensure that the webserver serving the laravel application has permission to write to the directories inside the `storagae` directory. If these directories are not writable by the webserver, no error messages will be displayed or logged.
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 {{code|.gitignore}} file set by default to exclude {{code|.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.


=== Miscellaneous Tools ===
By putting your passwords and settings into the {{code|.env}} file, you do not risk accidentally exposing your private information.}}
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
== Managing the Project using Artisan ==
npm install --global bower
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.


Then run on the root project directory:
{{Info|Create an Alias for Artisan|You may want to create an alias for artisan by running {{code|1=alias artisan='php artisan'}}}}
npm install


or extra plugins with:
To list all the available commands that Artisan can take, invoke artisan.
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/`
== Database ==
 
Laravel supports various databases through its ORM. You can set the default database connection and the database properties by editing the {{code|.env}} Environment file.
=== 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


== Database ==
=== Migration ===
=== 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.
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.
Line 56: Line 41:
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:
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:


<syntaxhighlight lang="php">
{{highlight|lang=php|code=
class DatabaseSeeder extends Seeder {
class DatabaseSeeder extends Seeder {


Line 81: Line 66:
     }
     }
}
}
</syntaxhighlight>
}}




== Application ==
== Application ==
Most of your project files will end up inside the `app` and `resources` directory.  
For most web applications, the business logic code goes under the {{code|app/Http}} directory.
 


=== 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.


=== Routing ===
=== Routing ===
All HTTP requests will go through to your `app/Http/` portion of your laravel project.
HTTP requests will go through the {{code|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 {{code|artisan route:list}}
 
Controllers, requests, and middleware are placed in their respective directory under {{code|app/Http}}.


=== Models ===
=== 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.
Models are placed directly in the {{code|app}} directory and under the `App` namespace. When working with Models, reference them directly by their namespace or pull in the namespace.


=== Views ===
=== Views ===
Laravel makes use of views through its Blade templating engine. Use the View facade to pull in views.


==== Forms ====
==== Forms ====
Line 116: Line 105:




== 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.
{{Highlight|lang=bash|code=
wget -O - https://getcomposer.org/installer {{!}} php -- --install-dir=$HOME/tmp/
}}
Normally, to invoke Composer, one would need to run
{{Highlight|lang=bash|code=
php $HOME/tmp/composer.phar
}}
To make this easier, create an alias instead.
{{Highlight|lang=bash|code=
alias composer='php $HOME/tmp/composer.phar'
}}
=== Updating ===
Use composer to update a project's dependencies by running {{code|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


[[Category:Coding]]
[[Category:Coding]]
[[Category:PHP]]
[[Category:PHP]]

Revision as of 01:09, 9 October 2015

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.

By putting your passwords and settings into the .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 running alias 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.

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.

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