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
|
|
Then run the following command to generate the Shop controller:
1
|
|
Open up /app/Http/Controllers/ShopController.php and replace the generated codes with the following codes below:
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 |
|
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:
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 |
|
So far so good, That’s it!!! See ya!!! :)