Laravel: Difference between revisions

From Leo's Notes
This page was last edited on 17 September 2019, at 04:09.
No edit summary
m prerequsites
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:


== Installation ==
== Prerequsites ==
Download composer and create an alias so that `composer` launches composer:
Some extensions that are required include:
wget -O - https://getcomposer.org/installer | php -- --install-dir=$HOME/tmp/
{{highlight|lang=terminal|code=
alias composer='php $HOME/tmp/composer.phar'
# yum -y install php-dom php-mbstring php-zip
}}


Set up the laravel project using composer:
== Creating a New Project ==
composer create-project laravel/laravel projectname --prefer-dist
Create a new Laravel project using [[Composer]]. Specify the directory to contain the new Laravel project.
{{highlight|lang=terminal|code=
composer create-project laravel/laravel --prefer-dist $PROJECT_DIRECTORY
}}


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:
=== Permissions ===
alias artisan='php artisan'
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.


The initial laravel project will come with scaffolding project files which you may want to remove by running:
=== Configuration ===
artisan fresh
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.


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.
{{Info|Always Edit the {{code|.env}} Environment File|Always make changes to the {{code|.env}} environment file instead of changing the config file directly.


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


or extra plugins with:
== Managing the Project using Artisan ==
npm install gulp-minify-css --save-dev
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.


==== Bootstrap ====
{{Info|Create an Alias for Artisan|You may want to create an alias for artisan by running {{code|1=alias artisan='php artisan'}}}}
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/`
To list all the available commands that Artisan can take, invoke artisan.


=== Troubleshooting ===
You can see what packages and dependencies were installed by running:
composer show -i


All HTTP routes can be listed using `artisan`:
== Database ==
artisan route:list
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.


== 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 47:
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 72:
     }
     }
}
}
</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. 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 {{code|key}} can be used in a parent template (from extending) in
@yield('key')
Variables can be recalled using {{code|{{ $variable }}}}


==== Forms ====
==== Forms ====
Line 102: Line 109:
  composer require illuminate/html
  composer require illuminate/html


Add the service provider to your config/app.php provider list and create a new facade.
Add the Illuminate\Html\HtmlServiceProvider to the service provider list:
Illuminate\Html\HtmlServiceProvider::class,
 
Then add the two class aliases:
  'Form' => 'Illuminate\Html\FormFacade',
  'Form' => 'Illuminate\Html\FormFacade',
  'Html' => 'Illuminate\Html\HtmlFacade',
  'Html' => 'Illuminate\Html\HtmlFacade',


In the blade templates, you can now use:
In the blade templates, you can now use:
  {!! Form::open() !!}}
  {!! Form::open() !!}
   {!! Form::label('name', 'Name:') !!}}
   {!! Form::label('name', 'Name:') !!}
   {!! Form::text('name', 'default', ['class' => 'blah'] ) !!}}
   {!! Form::text('name', 'default', ['class' => 'blah'] ) !!}
  {!! Form::close() !!}}
  {!! 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.
{{highlight|lang=terminal|code=
$ wget -O - https://getcomposer.org/installer {{!}} php -- --install-dir=$HOME/tmp/
}}
Normally, to invoke Composer, one would need to run
{{highlight|lang=terminal|code=
$ php $HOME/tmp/composer.phar
}}
To make this easier, create an alias instead by putting the following in your {{code|.bashrc}} file or by running it:
{{highlight|lang=terminal|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:
{{highlight|lang=terminal|code=
# npm install --global gulp
}}
You may also want to install bower, a package manager for web stuff
{{highlight|lang=terminal|code=
# npm install --global bower
}}
Then run on the root project directory:
{{highlight|lang=terminal|code=
# npm install
}}
or extra plugins with:
{{highlight|lang=terminal|code=
# npm install gulp-minify-css --save-dev
}}
==== Bootstrap ====
{{highlight|lang=terminal|code=
# echo '{"directory": "vendor/bower_components"}' > .bowerrc
# bower search bootstrap-sass
# bower install bootstrap-sass-official --save
}}
The bower files will now be in {{code|vendor/bower_components/}}
== Troubleshooting ==
You can see what packages and dependencies were installed by running:
{{highlight|lang=terminal|code=
$ composer show -i
}}


All HTTP routes can be listed using {{code|artisan}}:
{{highlight|lang=terminal|code=
$ artisan route:list
}}


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

Latest revision as of 04:09, 17 September 2019

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.

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