Sorry. Been busy. So what I would do is look at the toolbar
file as a bootstrap file for our final product. The main purpose of the toolbar
file is to pretty much instantiate the necessary files to do the task. Each task such as Edit
, Delete
, Update
, etc are all controllers. These controllers will then have 1 method called id
. In the id
method, we take in 1 argument that’s strictly an integer by type hinting it. We do not want strings being passed as the id
will only consist of integers.
So to do so, we must first configure our .htaccess
file to take in 2 parameters of the URL
. I typically call these “segments” once they are combined with forward slashes. So to do so, we’ll have to write something like so.
RewriteEngine On
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^toolbar/(.+)/(.+)$ /toolbar.php?action=$1&id=$2 [QSA,L]
RewriteRule ^toolbar/(.+)$ /toolbar.php?action=$1 [QSA,L]
The file toolbar.php
will take in 2 parameters, action
and id
. action
will later become our controllers and id
will tell the controllers which ID to use. The second rewrite rule is there for fool-proof in case someone types in /edit/
instead of /edit/1
. This should give you a clear picture of how we want the URL
to look. So this is where the whole “segment” part comes in. I see /edit/1
as 2 segments. edit
is the first segment and 1
is the 2nd segment.
Next, we will construct our toolbar.php
file. In order for us to do that, we must first check to make sure that both action
and id
exists within the URL
. If they both don’t, then use a 404 error page. Something like the bellow.
if(isset($_GET['action']) AND isset($_GET['id'])) {
// Both action and id exists within the URL, let's do something with them.
} else {
// Error page here.
}
Ok. So the first few steps are kind of easy. All we really do now is assign them with variables. We will give $_GET['action']
the variable of $action
and $_GET['id']
the variable of $id
. This should be relatively simple. Something like the below.
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = $_GET['id'];
} else {
// Error page here.
}
Now, we are going to type cast
the id
because since we are going to use type hinting
, we have to actually force the id
to be an integer. If we don’t, the actual data type of id
is actually a string no matter if we have only numbers in that variable. Next, we’ll also have to declare that we want to use strict typing and that actually has to go first before anything. Apparently even before namespaces
. So something like the below should suffice.
declare(strict_types=1);
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = (int) $_GET['id'];
} else {
// Error page here.
}
So now that we have our basic template done, we have to dig in further and actually do some dynamic calls. This is where it gets a little bit tricky if you don’t know what you’re doing. We’ll put all of our controllers in a folder called toolbar
. We’ll also make sure that the files actually exists so we’ll use file_exists
and throw that into an if
statement. So something like so.
declare(strict_types=1);
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = (int) $_GET['id'];
if(file_exists('toolbar/' . $action . '.php')) {
// We are good now.
} else {
// Error page here again.
}
} else {
// Error page here.
}
Next, we’ll then include the file using the require_once
function. Again, if you want to know why we aren’t using the include
function, you can just go look at the topic I linked before and read through it.
declare(strict_types=1);
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = (int) $_GET['id'];
if(file_exists('toolbar/' . $action . '.php')) {
// We are good now.
require_once 'toolbar/' . $action . '.php';
} else {
// Error page here again.
}
} else {
// Error page here.
}
This looks a bit promising, but this isn’t everything. People just stop at this point and just start writing their task files. I honestly think this is a bad idea. I am not a fan of mushing everything up into 1 single file. This is a terrible idea that will backfire hard. So we go further using OO
. Next, we’ll dynamically call a class, call the method, and pass in a variable within the method. This should be sufficient enough to do our tasks. So to dynamically call the class, we’ll have to know what we are going to be naming our classes. Personally, I would just use the task’s name and then capitalize the first letter and use that as the class name. Keeping it brief and simple. To do this, we can just use the ucfirst()
function. So, it’ll look something like this.
declare(strict_types=1);
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = (int) $_GET['id'];
$className = ucfirst($action);
if(file_exists('toolbar/' . $action . '.php')) {
// We are good now.
require_once 'toolbar/' . $action . '.php';
} else {
// Error page here again.
}
} else {
// Error page here.
}
If someone typed /edit/1
, the class name will actually be Edit
. If someone typed in /delete/1
, the class name will actually be Delete
. Next, we’ll dynamically call the class and assign it with a dynamic variable. Something appropriate for the class name and the file name. So we’ll just assign a variable based on the task name. To do this, it’s simple. We just wrap the $action
variable around curly braces and then put a $
in front of it to make it a variable. In PHP
7, everything is read from left to right. So in order for us to tell PHP
that we want to actually get the name of the variable first, we’ll have to wrap curly braces around it first. If we don’t do that, PHP
will execute from left to right. This will actually cause an error since $$action
isn’t a thing. By wrapping a curly brace around $action
, we tell PHP
that we want it to execute this piece first. This in turn will give us something like edit
or delete
. Next, PHP
will then execute from left to right and the end result will actually be something like $edit
or $delete
.
declare(strict_types=1);
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = (int) $_GET['id'];
$className = ucfirst($action);
if(file_exists('toolbar/' . $action . '.php')) {
// We are good now.
require_once 'toolbar/' . $action . '.php';
${$action} = new $className();
} else {
// Error page here again.
}
} else {
// Error page here.
}
Next, we’ll have to call the id
method so that we can pass in the id
and do some cool stuff with it. In order for us to do that, we must make sure that the id
method actually exists. We’ll use something called is_callable
. Some people prefer method_exists
, but we’ll just use is_callable
for now.
declare(strict_types=1);
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = (int) $_GET['id'];
$className = ucfirst($action);
if(file_exists('toolbar/' . $action . '.php')) {
// We are good now.
require_once 'toolbar/' . $action . '.php';
${$action} = new $className();
$callable = [${$action}, 'id'];
if(is_callable($callable)) {
// The method is accessible.
} else {
// Error page here once more.
}
} else {
// Error page here again.
}
} else {
// Error page here.
}
From here, now all we just really need to do is call the actual method and pass in the id
. So something like so.
declare(strict_types=1);
if(isset($_GET['action']) AND isset($_GET['id'])) {
$action = $_GET['action'];
$id = (int) $_GET['id'];
$className = ucfirst($action);
if(file_exists('toolbar/' . $action . '.php')) {
// We are good now.
require_once 'toolbar/' . $action . '.php';
${$action} = new $className();
$callable = [${$action}, 'id'];
if(is_callable($callable)) {
${$action}->id($id);
} else {
// Error page here once more.
}
} else {
// Error page here again.
}
} else {
// Error page here.
}
We are finally done with the toolbar.php
file. Now we have to focus on the task controllers. These controllers are actually really simple. It only requires 1 method and then we can pretty much do whatever we want with the passed in id
. So for each task, the controllers are going to be relatively similar.
For edit
.
class Edit {
public function id(int $id = 0) {
print($id);
}
}
For delete
.
class Delete {
public function id(int $id = 0) {
print($id);
}
}
Now, all we really need to do is the generic basic things such as form submission checking and form validation. Then displaying things and what not. But from here, you can pretty much work out the simple things. This is where you also call your database if you are dealing with one. But you get the gist of it.
But any comments is welcomed even if it’s criticism.