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 9004775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:58:28+00:00 2026-06-16T00:58:28+00:00

Possible Duplicate: CMS Routing in MVC I want to implement the MVC design structure

  • 0

Possible Duplicate:
CMS Routing in MVC

I want to implement the MVC design structure and currently struggeling with an good solution to parse requested views.

In my routing file, I have following code:

public function parseRequestedView() {

   $this->ressource_requested = explode('/', trim($_GET['view'], '/'));

   // e.g.: http://www.foo.com/article/{id}/comments/show
   if (!empty($this->ressource_requested[3])) {

      // Format: [0] viewpoint (article), [1] child (comments), [2] action (show), [3] reference ({id}),
      //         [4] additional information (from $_POST)
      return array($this->ressource_requested[0], $this->ressource_requested[2], $this->ressource_requested[3],
                   $this->ressource_requested[1], $_POST);

   // e.g.: http://www.foo.com/article/{id}/show
   } elseif (!empty($this->ressource_requested[2])) {

      return array($this->ressource_requested[0], NULL, $this->ressource_requested[2], $this->ressource_requested[1],
                   $_POST);

   // e.g.: http://www.foo.com/archive/show
   } else {

      return array($this->ressource_requested[0], NULL, $this->ressource_requested[1], NULL, NULL);

   }

}

The idea is, no matter what a visitor types into the browser, the function parses the request and always returns the same formatted array/output. The first segment of the URL following the hostname is always the main viewpoint (e.g.: article). In the end, I am including the view through another function called includeTemplateFile(). The files have this naming convention:

viewpoint.child.action.template.php
e.g.: article.comments.show.template.php

My question is now: Is there a more elegant solution? I read some of the turorials/articles (e.g.: http://johnsquibb.com/tutorials/mvc-framework-in-1-hour-part-one) regarding this topic, but I do not like most solutions since they are not well designed.

Here is the content of the .htaccess file:

RewriteEngine on

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

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

Thanks in advance.

  • 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-16T00:58:29+00:00Added an answer on June 16, 2026 at 12:58 am

    Ok I’m just gonna start by saying I’m no expert in PHP and I recommend using a framework such as symphony to do your routing, but here is one possible solution that you could use.

    function regexPath($path)
    {
        return '#' . str_replace([":int:", ":string:"], ["\d+", ".+"], $path) . '#';
    }
    
    function parseRequestedView($url)
    {
        $ressource_requested = explode('/', trim($url, '/'));
    
    
        // define our routes, and the indices that each route will use (from the exploded url)
        // this could be defined as another parameter or as a member of the class
        $routes = [
            regexPath("article/:int:/comments/show") => [0,  2, 3,  1], // will return array(resource[0], resource[2], resource[3], resource[1]), etc
            regexPath("article/:int:/show")          => [0, -1, 2,  1], // -1 will return a null
            regexPath("archive/show")                => [0, -1, 1, -1]
        ];
    
    
        // go through each route, checking to see if we have a match
        foreach ($routes as $regex => $indices)
        {
            if (preg_match($regex, $url))
            {
                // it matched, so go over the index's provided and put that data into our route array to be returned
                foreach ($indices as $index)
                {
                    $route[] = $index > -1 ? $ressource_requested[$index] : null;
                }
    
                // include the post data (not really nessesary)
                $route[] = $_POST; // unnessesary to pass $_POST data through function, because it is global
    
                return $route;
            }
        }
    
        return null; // or some default route maybe?
    }
    
    $route = parseRequestedView("article/13/comments/show");
    
    echo '<pre>';
    print_r($route);
    echo '</pre>';
    
    /* returns:
    Array
    (
        [0] => article
        [1] => comments
        [2] => show
        [3] => 13
        [4] => Array // this is our $_POST data
            (
            )
    
    )
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: turn URL route into funciton arguments php mvc CMS Routing in MVC
Possible Duplicate: PHP session seemingly not working I'm currently coding my own CMS for
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: Use grep to report back only line numbers I only want to
Possible Duplicate: using seo user friendly in php Custom routing in code ingniter I'm
Possible Duplicate: What good does zero-fill bit-shifting by 0 do? (a >>> 0) I
Possible Duplicate: How costly is .NET reflection? The elegant solution to a problem I
Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper

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.