Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8105165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:05:37+00:00 2026-06-06T00:05:37+00:00

I’m developing a simple php mvc that’ll do the bare minimum but work how

  • 0

I’m developing a simple php mvc that’ll do the bare minimum but work how I need it too, it’s my first time using an mvc approach over prodcedural so I’m learning as I go ..

While developing I’ve accidentally created it in a strange sort of style, currently the main .htaccess contains pretty much all of the physical rewrites, for example the forum is:

RewriteRule ^forum/([a-zA-Z0-9_]+)_([0-9]+)/$                    index.php?controller=forum&method=showThread&urlTitle=$1&threadId=$2 [L] 
RewriteRule ^forum/([a-zA-Z0-9_]+)_([0-9]+)/all/([0-9]+)$        index.php?controller=forum&action=showThread&urlTitle=$1&threadId=$2&page=$3 [L]

How it works at the moment is that all urls are directed to index.php and it then takes which controller and method to use from the url using:

index.php

$actionName = $_GET['action'];
$controllerName = ucfirst(strtolower($_GET['type'])).'controller';

$controller = new $controllerName;
$controller->$actionName();

controller/forumcontroller.php

class forumcontroller{

    function showThread() {

        $thread = new Thread($_GET['threadId'], $_GET['uriTitle']); 
        require "templates/thread.php";
    }

but this means it’s possible for users to go to locations I don’t want them to have access too, such as:

/public_html/templates/index.php

What I think I need?

I think instead the main .htaccess should look something like this?

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

and then in index.php you’d use something like:

$url = explode("/", `$_SERVER['QUERY_STRING']);`

$controller = $url[0];   //Returns "forum"
$data = $url[1];         //Returns the forum title and id

but with this approach I don’t understand how you call the action inside the controller with just the data?

wouldn’t you have to do something like:

 if(!$data)
     $controller->loadForum();
 elseif($data)
     $controller->loadForumThread($data);

Conclusion

I’m just not understanding how to best do the routing for a site that has a lot of seo friendly urls in different formats, I understand how an mvc should work but I’m struggling to grasp the routing part and all the examples I come across seem extremely complex!

I’m really struggling to see how to code the .htaccess and controllers to handle lots of urls in different formats, like this:

domain.com
domain.com/uploads
domain.com/profiles/username
domain.com/messages/inbox
domain.com/messages/createnew/userId
domain.com/forum/all/2
domain.com/forum/title_1/
domain.com/forum/title_1/all/3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:05:38+00:00Added an answer on June 6, 2026 at 12:05 am

    Here is an approach that is similar to your second .htaccess example.

    $request = explode('/', substr($_SERVER['REQUEST_URI'], 1));
    // Clean request array of empty elements
    foreach($request as $k => $v)
        // Clear any empty elements
        if(!$v) unset($request[$k]);
    $request = array_values($request);  // Renumber array keys
    

    This gives a numerically indexed array representing the requested URI. The application can assume that the first value in the request is the name of the controller:

    if(count($this->request) == 0) 
        $request[] = 'DefaultController';  // Responsible for homepage
    $this->controller = new $request[0]( $request );
    

    I also pass a $context variable to the controller constructor, but that’s out of scope for this question (it is responsible for database connection, current user data and session data).

    After that, it simply dispatches the request: $this->controller->dispatch()

    Inside of the dispatch method, the controllers themselves are aware of the request array. In your URL list, for instance, let’s look at the third example: domain.com/profiles/username:

    The controller would be named ‘Profiles’:

    class Profiles {
        private $request, $context;
        public function __construct($request, $context) {
            $this->request = $request;
            $this->context = $context;
        }
    
        public function dispatch() {
            if(count($this->request) == 2 && $this->request[1] == 'username') {
                // Load data from model for the requested username ($this->request[1])
    
                // Show View
            }
        }
    }
    

    There’s better ways that you can map request vectors to actions, but hopefully you get the jist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.