Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

PHP framework Laravel: Working with Routes

See in this article how to create your own routes using only Laravel PHP Framework

Laravel is a PHP framework created to develop web applications, has a simple documentation and a community of well wide and active developers. Is currently in version 5.1, which works with the MVC architecture (Model-View-Control), and provides us all the facilities you expect from a good framework, that is, we need not to worry about the infrastructure of our application, agility development, code reuse, among others.

Anyone who has developed something with the Ruby on Rails framework will have no trouble getting used to the Laravel, and even the most beginners because one of its main advantages is its low learning curve.

If you're interested, please take a look in another of our articles about Laravel: "An Introduction to Laravel Framework".

Prerequisites

To follow without a problem this article you must have installed PHP in any version from 5.5, in addition to extensions Open SSL PHPand PDO PHP, respectively, relating to security and database. Also a web server is required, such as Apache and a code editor or IDE. Finally, the Composer installation is necessary, that show step by step how to do.

Composer installation and Laravel on Linux

The first step is to open the terminal of your computer and run the following command:

curl -sS https://getcomposer.org/installer | php

After running this code, wait a few minutes and the download of the Composer should have ended.

After downloading, a file called composer.pharmust have been downloaded. We will copy it to a binary directory. For this, issue following the command:

sudo mv composer.phar /usr/local/bin/composer

Then, let's test if the composer is working, going to the terminal and running the following command:

composer #this command will be used more times

An screen like shown in Figure 1 must appear.

Composer command output

Figure 1. Output of composer command execution.

Now that our Composer is already working we will download the Laravel. We can do this in many ways, but here we will do via Composer, which is the fastest and most practical way, since in that way, at the end of the download, it has set its dependencies.

Again in the terminal go into the standard folder of your web server, which is usually /var/www/. Simply run the command:

cd /var/www/

Now run the following command, remembering that you must switch the part your-project-name for your own project name:

#Generic Command
composer create-project laravel/laravel your-project-name –prefer-dist
#Creating a project with the name contact
composer create-project laravel/laravel web-routes --prefer-dist

If you take a look at the folder /var/www/ you will see that there is a new folder with the name of your project. To this article the project was named of web-routes. When you enter it, you should see a number of other folders, but two deserve attention:

  • public, which is the project folder where the user will have access, thus making the project safer;
  • and storage, which will be saved the exception files, log files and several others used by the framework itself, so the framework must have full permission on this folder.

To change the permission of this folder, use the following command:

sudo chmod -R 777 /var/www/web-rotas/storage

Thus, the Laravel is already installed and we already have our first project created, and to test that everything is working correctly, go into your browser and enter the address of your application, in this case is http://localhost/web-routes/public/.

If everything is working correctly you should see the following screen in Figure 2.

Initial screen of the project

Figure 2. Initial screen of our project.

Downloading and installing the Composer and the Laravel in Windows

For those who are using the Windows system, go to the Composer site (see Links section) and click the Getting Started tab. Look for the section Installation - Windows, where you should see a link to download the Composer Setup. After downloading click the installer and follow the installation process.

To test if the Composer is installed properly go to the command prompt and type:

composer

The output should be something like has been shown in Figure 1.

Now let's install Laravel, entering in its Github repository (see Links section) and download the entire project.

Then, unzip the downloaded file and copy the laravel-master folder to your web server if you are using Wamp, the default folder is C:\wamp\www. After copied, renamethe folder to contact. Be sure to copy the address of the framework C:\wamp\www\contact. Upon returning to the command prompt, access this folder using the command:

cd c:\wamp\www\contact

Now that we are in the command prompt inside the directory of our framework, type the command:

composer install

After running this command it will fetch all the dependencies of our framework and will make your installation.

Now that we have our framework and all its dependencies installed we'll test if everything is working properly by accessing the browser address of your project, which is http://localhost/contact/public.

The result should be the same as in Figure 2.

Working with Routes

Now that you've installed the framework and we have created our application, we will learn how to configure our application to provide responses when a user visits certain URLs of our project, ie we'll create routes.

As we enter the URL of our project we are directed to a default home page, as shown in Figure 2. Who says what is the default page that we access is the routes.php file that is in the folder app/Http/.

Using a code editor or IDE of your choice open this file, you will have the same content present in Listing 1.

Listing 1. routes.php Content.

  <?php 
   
  /*
  |--------------------------------------------------------------------------
  | Application Routes
  |--------------------------------------------------------------------------
  |
  | Here is where you can register all of the routes for an application.
  | It's a breeze. Simply tell Laravel the URIs it should respond to
  | and give it the controller to call when that URI is requested.
  |
  */
   
  Route::get(’/’, function () { 
   
  return view(’welcome’); 
   
  });

This is the content of routes.php file of the Laravel 5.1, but if you are using an earlier version content may be something like Listing 2.

Listing 2. Contents of routes.php in versions prior to 5.1 Laravel.

  <?php 
  /*
  |--------------------------------------------------------------------------
  | Application Routes
  |--------------------------------------------------------------------------
  |
  | Here is where you can register all of the routes for an application.
  | It's a breeze. Simply tell Laravel the URIs it should respond to
  | and give it the controller to call when that URI is requested.
  |
  */
   
  Route::get(’/’, ’WelcomeController@index’); 
   
  Route::get(’home’, ’HomeController@index’); 
   
  Route::controllers([ 
  ’auth’ => ’Auth\AuthController’, 
  ’password’ => ’Auth\PasswordController’, 
   
  ]);

In Listing 1 we see that there is already a route set, it concerns the exact starting page of our project. Now, erase all that code so we can create our own route.

We must use the Route::get, so that the routes file can stay as in the code in Listing 3.

Listing 3. Creating my first route.

  <?php
   
  Route::get('/', function () {
      return 'My first route with Laravel';
  });

Now if you go back to the browser and refresh your project you will see that your home page is not the same.

This is because we are initially calling the get method of the Route class that is receiving two parameters: the first is the path (or route, if you prefer) that the browser will access; and the second parameter is a function that will give an answer to the browser.

So, we are saying to our web server when someone access the folder '/', ie the root folder of our project, should run a function that returns the text "My first route with Laravel".

In our function we pass a common text, but we can pass an HTML content, for example, as shown in the example in Listing 4.

Listing 4. Route with HTML content.

  <?php
   
  Route::get('/', function () {
      return '<h1>My first route with Laravel</h1>';
  });

We can create as many routes as needed to our application, however, a common problem is the conflict of routes, eg.

Let's create a second route as shown in Listing 5.

Listing 5. Example of conflicting routes.

  <?php
   
  Route::get('/', function () {
      return '<h1>My first route with Laravel</h1>';
  });
  
  Route::get('/', function () {
      return '<h1>My second route with Laravel</h1>';
  });

Conclusion

In this code we see that there are two routes, but the problem is that both are pointing to the same directory '/' and, therefore, if we try to re-access our page the browser will see that the second route is overwriting the first. So we must be very careful with the address passed to our routes.

Links

Official Composer WebSite: https://getcomposer.org/

Official website of Laravel: http://laravel.com/

GitHub repository of Laravel: https://github.com/laravel/laravel

Official download page for MySQL: http://dev.mysql.com/downloads/mysql/

MySQL Installation Tutorial: http://dev.mysql.com/doc/refman/5.7/en/installing.html




Julio is a System analyst and enthusiast of Information Technology. He is currently a developer at iFactory Solutions company, working in the development of strategic systems, is also a JAVA instructor. He has knowledge and experi...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login