Laravel 5.x.x Route & SEO

Laravel 5.x.x Route and SEO

SEO stands for “search engine optimization”. URLs is an important thing in getting found on the web. In this article I will implement routes and SEO friendly URLs for Laravel project.

Things that Affect SEO
The following are some of the things that search engines such as Google Search consider when evaluating web sites:
1. Website speed
- No one waiting to visit a websites that take forever to load. We all love fast websites. The goal should be to keep the load time under 2 seconds. If you can get it under a second that is even much better. You need to test your web application for speed and optimize if necessary.
2. Responsive designs
- Mobile devices have a market share of internet usage. Since user experience matters to search engines, you need to ensure that the web site displays properly in mobile devices, tablets and desktops as well.
3. Keywords
- Search engines look at keywords when querying billions of indexed websites. As a developer you have to ensure that you provide title tags, meta description and HTML H2 heading that the content writers can use to place keywords.
4. Social media statistics
- If you read something cool on the web, you naturally share it on social media. This is a stamp of approval to search engines. Your have to include tools on the web site that will make it easy for the visitors to share the contents.
5. Website URLs
- The URLs should be keyword rich and words should be separated by dashes and not underscores.

How to Implement SEO Friendly URLS in Laravel
Now we have to cover the basics SEO and we will map routes to controllers and create a single controller for all routes. The following table shows the URLs that will be implemented:

# URLs Method Description
1 / index Home page
2 /products products Products page
3 /products/details/{id} product_details(id) Product detailed based on product id
4 /products/category product_categories Product categories
5 /products/brands product_brands Product brands
6 /blog blog Blog postings list
7 /blog/post/{id} blog_post{id} Blog post content
8 /contact-us contact_us Contact us page
9 /login login Login user
10 /logout logout Logout user
11 /cart cart Cart contents
12 /checkout checkout Checkout shopper
13 /search/{query} search Search results

For this section assumes you have created the tutorial project. If you haven’t done so yet then read this Laravel Hello World. We use the artisan command line tool to generate the codes for ShopController.php controller.

Then open up your terminator and run the following command to browse to the project. Assumed that you are using Laravel plugin web server.

1
php artisan serve

Then run the following command to generate the Shop controller:

1
php artisan make:controller ShopController

Open up /app/Http/Controllers/ShopController.php and replace the generated codes with the following codes below:

ShopController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ShopController extends Controller {

  public function index() {
    return 'index page';
  }

  public function products() {
    return 'products page';
  }

  public function product_details($id) {
    return 'product details page';
  }

  public function product_categories() {
    return 'product categories page';
  }

  public function product_brands() {
    return 'product brands page';
  }

  public function blog() {
    return 'blog page';
  }

  public function blog_post($id) {
    return 'blog post page';
  }

  public function contact_us() {
    return 'contact us page';
  }

  public function login() {
    return 'login page';
  }

  public function logout() {
    return 'logout page';
  }

  public function cart() {
    return 'cart page';
  }

  public function checkout() {
    return 'checkout page';
  }

  public function search($query) {
    return "$query search page";
  }
}

The above code defines functions that will responds to the routes.

And then we will add routes that will call the methods in the controllers.

Open up web.php in /routes folder and replace the code with the following:

web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/','ShopController@index');
Route::get('/products','ShopController@products');
Route::get('/products/details/{id}','ShopController@product_details');
Route::get('/products/categories','ShopController@product_categories');
Route::get('/products/brands','ShopController@product_brands');
Route::get('/blog','ShopController@blog');
Route::get('/blog/post/{id}','ShopController@blog_post');
Route::get('/contact-us','ShopController@contact_us');
Route::get('/login','ShopController@login');
Route::get('/logout','ShopController@logout');
Route::get('/cart','ShopController@cart');
Route::get('/checkout','ShopController@checkout');
Route::get('/search/{query}','ShopController@search');

So far so good, That’s it!!! See ya!!! :)