2

So I've been using a procedural programming style in PHP since 2006. But missed out on a lot of time the past 4+ years with getting any better.

Recently I've started to acknowledge object oriented programming more.

The thing that points out to me with it the most, is the MVC style organization and short urls.

instead of url being "index.php?page=profile&member=1", it would be "page/profile/member/1".

I'm trying to learn OOP&MVC architecture with using a wide range of resources, but it is still a bit too much for me to grasp. Even after a month!

I've always just used basic function(etc) {}, if,else,and,or statements and SQL queries to get the job done. That knowledge up til now has mostly covered everything I needed.

I'm wondering if there is a way to use my basic procedural programming style and still achieve shortened urls?

I've tried this method below

.htaccess

RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?page=$1 [L]

index.php

<?php

$links = parse_ini_file('links.ini');

if (isset($_GET['page']) && array_key_exists($_GET['page'], $links)) {
    require $links[$_GET['page']];
}
else {
    echo '
    Home page or unknown link
    ';
}

links.ini

members  = pages/members.php
profile  = pages/profile.php

This method works on simply shortening a URL, but I don't know how to make it work with profile/user/1 aka index.php?page=profile&user=1 (the user variable being a database query)

Any help would be greatly appreciated!!!

Nasonix
  • 21
  • 2
  • 1
    The keyword (nowadays) that you're looking for, is: routing. A basic tutorial you can find here: http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/, but with some googling you'll find much more advanced stuff. – Blaatpraat Oct 05 '16 at 12:57
  • This is a hard tutorial to wrap your head around in the beginning, at least it was for me, it helps you make a simple router... https://www.youtube.com/watch?v=OsCTzGASImQ However I strongly suggest getting into a framework (and learning about composer) because they have inbuilt routers that are much more reliable then anything a learner could produce... – Jethro Hazelhurst Oct 05 '16 at 12:59
  • Just accept the concept of MVC is a little hard to grasp, but stick with it. Pick a popular modern framework (popular is important as you will need a good community to ask questions, find tutorials etc) and make something from scratch, such as a basic blog. I would suggest Laravel with the aid of laracasts (very good video tutorials, most of the beginner ones being free). There are a lot of things to a decent framework beyond a routing system. Once you understand autoloading and orms you are unlikely to go back to procedural vanilla php except for one off shell scripts – Steve Oct 05 '16 at 13:04
  • Thanks a lot! I'm going to go over this stuff right now. I actually do have composer, and use it a lot already. The only problem I face is not understanding the MVC framework and OOP code style that all of it's applications use. it's strange how I can't wrap my head around OOP code, even after a month long of trying, but procedural programming seemed so easy for me to understand – Nasonix Oct 05 '16 at 13:04
  • https://laracasts.com/series/laravel-5-from-scratch – Steve Oct 05 '16 at 13:05

1 Answers1

0

You can accomplish this with what is known as a front controller. This Sitepoint article may help the concept sink in, but it doesn't have to be object oriented to pull it off.

Basically, rather than do any actual rewriting you would just direct all requests to index.php:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php

Now, within index.php, you can pick the request apart with parse_url and handle the request however you want.

mister martin
  • 6,197
  • 4
  • 30
  • 63