Treceți la conținutul principal

Laravel 5 "Hello World" example using Xampp and Composer

This is a tutorial about creating your first project in the Laravel framework and running it with the help of Xampp. This tutorial has been done for Laravel 5 and, since then, some of the framework's files and folders have changed but the process of setting up the project is identical so you can still use it today.
Laravel
Image 1 - Laravel welcome page - image by C.R.G.

You can download the project via Git here.

Here's the youtube link to a similar tutorial.

Other links you may want to visit:
Laravel docs
Xampp download
Composer download
Notepad++ download

Also, you can learn more abour laravel and model-view-controllers on wiki.

First, I'll start by explaining what is Laravel.

  Laravel is a free and open-source PHP web framework, intended for the development of web applications following the MVC or Model–View–Controller architectural pattern. 
Some of the features of Laravel are a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, utilities that aid in application deployment and maintenance.

What is a Model–View–Controller? Do I hear you ask? Well, actually, I haven't, but I'll tell you anyway :P.
  The MVC architectural pattern is a software architectural pattern for implementing user interfaces on computers. It divides a given application into three interconnected parts. 
This is done to separate internal representations of information from the ways information is presented to, and accepted from, the user.

Now let's get going with our tutorial..


  An important thing before starting any projects is learning the tools of the trade. I mean, as humans, we need tools..this is what separates us from animals :)).
As seen at the start of this tutorial, Laravel is php web framework, so you'll naturally need php for it to work. Now, that can be achieved in several ways, but the most easy way is to use a solution that simulates the environment needed by Laravel. The solution I use is XAMPP which simulates an Apache HTTP Server with php and  mysql, so exactly what we need.
I suggest you use the same solution to easily understand what this tutorial tries to achieve.
Laravel needs Composer to handle its dependencies, so we'll need that, as well.
We'll also need a text editor, but for that you can use anything you want. I use Notepadd++, but you can even use notepad or wordpad ( windows ).
If you want to dive into web programming I assume you allready know how to download and install a program so we'll move forward with this tutorial.

  We'll use as a show-and-tell tutorial a project I've created and uploaded on Git. If you haven't used Git before, don't worry, you won't need it for this tutorial. I'll explain the program, and how to download it via Git, in a future tutorial.

  Let's continue..you have installed Xampp and Composer. You can install them anywhere you wannt but I've installed them in drive "C:\" so if the paths shown in this tutorial don't match with yours, just modify them accordingly.

Configuring Xampp:

  Open "\xampp\apache\conf\extra\httpd-vhosts.conf" or 
"C:\xampp\apache\conf\extra\httpd-vhosts.conf" ( for me ) and add at the bottom of the file the code below (the paths and folder names can be changed to your respective paths):

# VirtualHost for LARAVEL.DEV

<VirtualHost laravel.dev:80>
  DocumentRoot "C:\xampp\htdocs\laravel_h_w\public"
  ServerAdmin laravel.dev
  <Directory "C:\xampp\htdocs\laravel_h_w">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
  </Directory>
</VirtualHost>

 What is important for us in the script above? At 'DocumentRoot' we type the path to the folder where a laravel project usually holds the 'index.php' and other files needed by Xampp. At 'ServerAdmin' the address to the localhost is written 'laravel.dev' for us, but you can use a different one. This address is not configured yet; And at 'Directory', the path to 
the root of the project needs to be added.

Configuring the 'hosts' file. ( under Windows )


  As you've seen from the script above, our Apache server will listen to 'laravel.dev' connections. In order for our example to run as intended we will need to configure our 'hosts' file so that the 'laravel.dev' address will redirect to the localhost.
To do that we navigate to "C:\Windows\System32\Drivers\Etc\" ( you drive can have a different letter )and open the 'hosts' file with a text editor. If you use Windows 7, or newer, you'll have to run your text editor as administrator.
Now add this line at the bottom of the file:

127.0.0.1       laravel.dev

  OK. Everything is configured and ready to go...but wait! Where is Laravel? Let's install it. To do that open 'Command prompt' navigate to "\xampp\htdocs\" and write the following command:

composer create-project laravel/laravel laravel_h_W ( you can name the project differently )

Now laravel should install. It takes a while so be patient.
After the installation is done, we can launch Laravel for the first time. To be able to do that, first, we need to simulate that server environment that Laravel requires. We do that by initializing Xammp. Open 'Xampp Control Panel' and under the 'Actions' label hit 'Start' for Apache and MySql. Now open your internet browser and type 'laravel.dev' in the address bar.
Voila! Laravel is running unhindered ( see image 1 for refrence ).

  Great! You made your first laravel project run. But I don't think you wanted to know how to create a laravel project just to see its welcome page. You probably want to use it in the near future, and to do that you must familiarize yourself with its structure and files. To better get acquainted with laravel we will modify its welcome page to show what we want, or shall I say, what I want. This tutorial is about a hello world type program so we'll write 'hello world' on the welcome page.


laravel

Image 2 - Laravel, default project structure - image by C.R.G.

  Now that we've seen the default structure of a laravel project, we can move forward by modifying a bit of code so we can see how everything works. As seen in the image above, or on Laravel's documentation :P, the views contain the HTML served by your application and separate your controller / application logic from your presentation logic. In plain English that means that all the data, including data from the databases, is rendered on the pages within the views folder. For Laravel to recognize these files they need to be saved with the ".blade.php" extension.
  There are some blade files included in any Laravel project by default so we'll modify one of them.
The page you see when you run a default laravel project is created in the welcome.blade.php file.
As I've said before, that can be found in the 'views' folder so go to: "xampp\htdocs\laravel_h_w\resources\views" open 'welcome.blade.php' and replace line 43 with the code below:

<a style="text-align:right;" href="{{ url('/auth/login') }}">Login</a>
<div class="title">Hello World from Laravel!!</div>

  Everything is set now, so open your internet browser and type "laravel.dev" in the address bar.
You're welcome! :P


laravel / crg
Image 3 - Laravel "Hello World" example by C.R.G. - image by C.R.G.


  That being said, I want to send a big thank you to Jeff Davis for his work on the laracasts forum. See post that spared me from a lot of headaches here.
and to Marc Garcia on codementor.


C.R.G. 2017

Comentarii

Postări populare de pe acest blog

Changing the processor on a LGA775 motherboard

Fig. A First of all, please read the disclaimer at the bottom of this page. You should also know this tutorial is intended for the more popular air coolers that normally contain a fan and a radiator, like shown in the image on the right. Now, with that out of the way, I'd like to explain a few things about the LGA775 motherboard and compliant processors. Unlike older socket types ( Socket 478 for example ), the LGA 775 motherboard introduced protruding pins instead of the multiple socket holes found in the past ( see fig. 1 ). There are 775 pins, hence the name. As expected, the compatible processors don’t have multiple pins on them like older processors, instead having just connectors arranged in the same pattern as the pins on the motherboard ( fig. 2 ). Fig. 2   Fig. 1 What will we need for this task? I hear you ask.. A LGA775 compatible processor ( mine is a 2.9GHz x86 Intel Pentium Dual-Core ); Thermal paste ( I've used MX4 thermal compound - the manufacturing

Using Visual FoxPro and the ODBC Driver to connect to a MySql Database

What you'll need: 1. Ms Visual FoxPro ( any version since 6.x will do but I’ve used 9.0 ); 2. XAMPP ( to set up an Apache web server and a MariaDB database server ); 3. MySql ODBC Driver ( I’ve used v.5.3 but any version should work ); 4. Creating a VFP script that sends MySql commands to the MySql server;   FoxPro by Carlosgutierreztobon17, CC BY-SA 4.0 via Wikimedia Commons   1. Ms Visual FoxPro You’ll obviously need to install VFP for this operation. I can supply you with a standalone version of the script but it won't be free as it needs to be custom made for your requirements ( if you still want this check the link at the bottom of this tutorial ). 2. Setting up XAMPP XAMPP is required if you don’t already have Apache and MySql servers installed. It will contain both of these modules and you won't have to worry about installing and configuring them. Use this link: https://www.apachefriends.org/download.html and select the correct version depending of your O