Check Order status | Verified Sales | Escrow Service | Advertise
westernunion carding gold carding
revolut carding electronics carding
paypal carding payoneer carding
advertise on cracking forums

PHP:MVC - Model View Controller | Front Controller #2 & Router Interfaces

Prince

[ Verified Seller ]
Staff member
Trusted Seller
Joined
10 yrs. 6 mth. 15 days
Messages
5,387
Reaction score
18,373
Age
44
Wallet
11,590$
Introduction:

First thing we gonna do is to unify the way that the routers are returning the information to the front controller so as to be unique and give us the opportunity for future changes and alongside that, with an ease. This case of returning data won't be with objects or methods but with URI. The URI will be the string that the front controller will parse. So let's contemplate a bit here:

Code:
Please, Log in or Register to view codes content!
Let's say this is our URL address. We have the model administration with the controller user with the method edit and some parameters afterwards. So basically, all this is just a string which contains actually everything we need for the front controller. Actually, we have that extraction in our parse method of the controller's class:

Code:
Please, Log in or Register to view codes content!
So from the whole request that is coming from the browser we just need to remove the domain name and, of course, evaluate the URL as string beforehand so as to manipulate it as such.

Code:
Please, Log in or Register to view codes content!
In the previous tutorial we ended up with the following code for the router's class. In our case we want to extract the URI which we explained and talked about above. Following that idea, the whole process can be quite shortened and we can end up with the following structure:

Code:
Please, Log in or Register to view codes content!
It is always better and therefore recommended that you develop things in a simpler way. With just one line of code as of our method, we did a job that could include a lot more LOC. The previous code had the idea of obtaining, parsing and returning the corresponding data so every router should have parsed the data as well. Whereas now it's idea is altered. So far we have just one default router but in the future series, every router will need to be able to unify the address so that the front controller may work with it.

Let's create an interface in the Routers' folder:

Code:
Please, Log in or Register to view codes content!
Within this interface we need to specify its task. That from its side would mean the contract it needs to accept and here we say that every object that implements IRouter must have a public function. Here we don't have a realization of the function. We just say that every class or object which implements IRouter should have that method.

Code:
Please, Log in or Register to view codes content!
Now back to our default router we'll make the implementation so as to work. If we don't specify the method within the class, we'll end up with this error which is self-explanatory:

Code:
Please, Log in or Register to view codes content!
So by including the method, we enforce it to be implemented. As we've mentioned in the previous tutorials there are three main things we need to follow along the series. The first one was to terminate all dependencies between parameters and name of the file, second rule was more than one address to be able to match a controller and the third one was to use some sort of packets or something similar. Therefore, we need to describe (code) those rules somewhere. Best directory for that would be the config one obviously.

Make a new routes.php file so as to keep with the tutorial:

Code:
Please, Log in or Register to view codes content!
That's the router by default. Now let's make a new controller with the namespace of Controllers and then say in routes that the controllers are in the namespace of Controllers, then things turn a lot easier for everything from now on.

Code:
Please, Log in or Register to view codes content!
So for example, if we get a request like:

Code:
Please, Log in or Register to view codes content!
It will be searched for in that namespace for the administrators. Basically, seeking for a controller called administration and is located in the namespace Controllers. On the other side, if we get something like that in our URL as a request:

Code:
Please, Log in or Register to view codes content!
The controller will search for the class Users, not in Controllers but in Controllers/Admin. So that is why we structured it like so as we said before that it would be easier in the further development. Now we need to code a way in which the front controller can communicate with the routes.php file and extract certain data from within it. And we'll start off with pointing where the namespace controllers is located.

app.php:

Code:
Please, Log in or Register to view codes content!
Now if we execute it like so, we'll get an error that registerNamespaces() is not located:

Code:
Please, Log in or Register to view codes content!
So just implement it in the Loader.php file:

Code:
Please, Log in or Register to view codes content!
 
Top Bottom