1

Hi I'm currenly playing around with PHP MVC programming, and was wondering if anyone has made some sort of "routing" with a database?

I have a "page" table that looks like this: https://i.sstatic.net/KKeFO.png

Currently all routes are hardcoded, and I thought it must be possible to do with a database..

But not only do I want to get the page and show it, I also need to be able to send parameters with it.

Example: As shown in my page table, I have a "test" url. If i type http://demo.com/test/ I would get "rerouted" to use the "home" controller and "Index" method. But I also need to be able to type http://demo.com/test/id/40 and id/40 will be sent as params to the controller/method.

If this isn't a good thing to do, or if anyone got a better soloution please let me know! :)

Regards, Frederik

2
  • Such thing is typically done a level lower, whilst rewriting the incoming request inside the http server. So before php is invoked. For the rewriting step there are solutions based on databases. But all that is questionable. Since all controllers and actions ahve to be coded anyway and the call signatures must match the action signatures anyway there is typically little advantage of such an approach. Why would one want to change the routing in such a dynamic manner? Things like "test" and "internal" are typical temporary examples. But those can easily be implemented by temporary rewrite rules. Commented Aug 24, 2015 at 19:06
  • I wouldn't structure a MVC framework around any database. Instead, use a framework like Laravel and create code that parses your URLs and loads the relevant file/controller/view/whatever. You could still use a database to hold your routes, but it would be separated from the framework. Commented Aug 24, 2015 at 20:34

1 Answer 1

1

This definitely depends on the server you're using, but since you're a PHP MVC noobie I'll reference apache in this example, and hope it's what you have for the sake of the examples.

First, you'll need your webserver configured to know that it has to send all traffic through your base page (usually index.php). Now that page would do some other stuff (call bootstrap, etc) but for the sake of argument we'll say that all it does right away is look at the request from the page, compare it to the DB, and complete the request if it can.

In that case, it will be helpful to have the request info from the server passed in to the index.php page. To do this, you'll want to configure apache with an .htaccess file similar to:

DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]

This tells it to use index.php for all requests that aren't specific files or directories, and to pass the full url path through to index.php as a $_GET var.

Next, in index.php, you'll want to check the path you were passed against DB of paths. Here's a really simple example to show:

<?php

// Obviously use your database and some string parsing here to match correctly
// I generally explode the path on '/' to break it into controller, action, etc.

if ($_GET['path'] == "user/account") {

    // Then you call the controller that matches the first part of the route
    // The action that matches the second part of your route
    // And pass the request along so you can access anything else

    call UserController::accountAction($_REQUEST);

} else if ($_GET['path'] == "user/resetpassword") {

    call UserController::resetPasswordAction($_REQUEST);

}

From there, you should be in the right place and have everything you need. That Controller/Action URL format is a fairly common one for how easily it lets us do this.

Hope the answer helped!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.