Friday, November 4, 2016

Howto Install Laravel Framework using Composer

Laravel framework provides rapid development of a model-view-controller (MVC) with PHP programming language. Currently is at version 5.3 and is open source using MIT license.

Composer is a tool to easily retrieve packages required for most PHP projects and frameworks. Laravel can be found available in Composer at packagist.org. Detailed documentation for Composer can be found at https://getcomposer.org

This is a quick guide on installing Laravel then creating a page to display hello world!.

Installation environment for this How-to;
MS Windows 10
Apache2
PHP v7.0.12
Composer 1.2.2
Laravel 5.3

By the end of this guide, the following directories should have been generated.


Step 1: Installing Composer

Ensure Apache2 and PHP7 is already installed and working before starting this guide.

Download and install Windows Composer installer https://getcomposer.org/Composer-Setup.exe

Open a terminal and change to the installed PHP folder. Type

 $> composer -V  

(Or with the file composer.phar in same directory, type)
 $> php composer.phar  
This should display the version number of composer.
Update composer and check the version.
 $> php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"  
 $> php -r "if (hash_file('SHA384', 'composer-setup.php') === 'aa96f26c2b67226a324c27919f1eb05f21c248b987e6195cad9690d5c1ff713d53020a02ac8c217dbf90a7eacc9d141d') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"  
 $> php composer-setup.php  
 $> php -r "unlink('composer-setup.php');"  
 $> composer -V  

Step 2: Create an empty composer project 

Create a project folder called myproject.
Open a terminal and enter folder for myproject
Type
 $> composer init  

Choose default values, except following;
Package type: project
dev dependencies: yes
Search for a package: laravel/laravel

Press "Enter" until exit the interactive menu.

This will create a default json file for the composer. This step is only an example of creating a json file. Feel free to ignore this step.

Step 3: Install Laravel Framework and dependencies

Create a project folder called myproject, if you haven't.
Open a terminal and enter folder for myproject, and type.

 $> composer create-project --prefer-dist laravel/laravel hello  


This will do pretty much that same as Step 2 above, except that it will create the workspace called hello and load all the required libraries and plugins.

Laravel is installed in the folder hello.

Step 4: Start local server

Open a terminal and enter folder for hello, and type.

 $> cd hello  
 $> php artisan serve  

Laravel development server started on http://localhost:8000/

Open a web browser and type the URL mentioned above.
http://localhost:8000/

It displays the default Laravel page.

Step 5: Display a Hello World page

Open a terminal and go to the project folder ->hello ->resources ->views
Create a file helloworld.blade.php with following text

 <!DOCTYPE html>  
 <html>  
   <head>  
     <title>Laravel</title>  
   </head>  
   <body>  
  Hello World!  
   </body>  
 </html>  

Tell Laravel that to display this "VIEW" when the URL is /hello.
Edit the file web.php in the project folder ->hello ->routes

 Route::get('/hello', function () {    
      return view('helloworld');  
 });  

Save the file.

Open a web browser and type the URL mentioned above.
http://localhost:8000/hello

This displays the Hello World! page with HTML formatting from the file resources/views/helloworld.blade.php

Step 6: Display a Hello World text (without Views)

Tell Laravel to display some HTML when the URL is /hello2
Edit the file web.php in the project folder ->hello ->routes

 Route::get('/hello2', function () {    
      return 'Hello World 2!';  
 });  

Save the file.

Open a web browser and type the URL mentioned above.
http://localhost:8000/hello2

This displays the Hello World 2! text.

Happy trying out Lavarel.



No comments:

Blog Archive