What Route Should I Go With?

The blog looks beautiful but the url looks ugly. What looks better this:

/?id=18

or

/building-a-blog/what-route-should-I-go-with

The second one is easier to read and looks way more descriptive than just a number. If only there was some cool kind of tool to use to handle this for me.

Well there is. It's called Klien and I'm getting ready to install it and use it in this blog. So get ready for some pretty urls.

First thing is to install Klien.

php composer.phar require klein/klein

And now I can use the tool to create the routes. But first I need to create the links I want on the website.

So in the aside bar I've updated the menu links.

<li><a href="/building-a-blog/<?= $blog['blog_permalink'] ?>"><?= $blog['blog_title'] ?></a></li>

So I'll call the klient tool.

$klein = new \Klein\Klein();

Klien uses the MVC pattern and separates the view from the controller. If I create a views directory I can store my HTML in that folder for my pages. I'll create a home.php file and paste my home page HTML in it.

I'm gonna be reusing some of the HTML in all the pages so I'll create a partials directory to put my reusable HTML. And I'll create a file called header.php for the top of the HTML pages. And I'll create one called footer.php for the bottom of the HTML pages.

I'll create a view for the blogs and call it blog.php. And I can reuse my header and footer HTML. I'll also create a category view so I can view all the blogs in a single category.

Next I'll create three page routes. One for the home, category, and blog page. And since I'm handling the urls differently I'll update the links to the blogs to be /blog-category/blog-title. And that will make for some very sexy urls. These aren't pretty urls they're sexy.

I'll update my previous blog and next blog functions to use the sexy urls. And I'll update my side bar menu to use the sexy urls.

Wow that was actually a little difficult. But the rewards are worth it. I hope you have a very Merry Christmas!!!

Comments