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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:59:14+00:00 2026-06-09T15:59:14+00:00

On working PHP MVC, on CONTROLLER folder i have a files controller, but inside

  • 0

On working PHP MVC, on CONTROLLER folder i have a files controller, but inside has folders that must called controller inside folders.

Has anyone better way and nicely code implementation of these below?

I’m working on this way but I’m not sure is that best way calling controller?

/*

STRUCTURE OF DIR FILES: controllers:
a/b/c/file.php
a/b/c <-- IS DIRECTORY
a/b/c <-- IS FILE

*/
$uri_result = false;
$controller_called = false;

/* called controllers */
$uri_segments[0] = 'a';
$uri_segments[1] = 'b';
$uri_segments[2] = 'c';
#$uri_segments[3] = 'file.php';
/* end called controllers */

$counted = count($uri_segments);

$filled = array();
$i = 0;
do {

    if ($i < $counted)
    {
        $z[] = $uri_segments[$i];
        $ez = implode('/', $z);
    }

    if (file_exists($ez))
    {
        $uri_result = $ez;
        $controller_called = $z[$i];    
    }
++$i;
} while ($i < $counted);

var_dump($uri_result,$controller_called);

/* RESULTS:

If called $uri_segments[0] to uri_segments[3]
string(14) "a/b/c/file.php" string(8) "file.php" 

If called $uri_segments[0] to uri_segments[2]
string(5) "a/b/c" string(1) "c" 

*/
  • 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-09T15:59:16+00:00Added an answer on June 9, 2026 at 3:59 pm

    The fundamental approach seems OK, but there’s a few things I’d tidy up, including better variable names and scope, and using a more appropriate loop.

    Also, from what I can see, you want to find the longest section of the URL path to act as the controller, so probably want to start long and get shorter rather than starting short and getting longer.

    /**
     * Find the most specific controller file to serve a particular URL path
     *
     * @param string $url_path Relative path passed through from URL handler
     * @return string $controller_file Relative path to controller file
     */
    function find_controller_file($url_path)
    {
        $url_parts = explode('/', $url_path);
    
        // Start with the full path, and remove sections until we find a controller file
        while ( count($url_parts) > 0 )
        {
            // Concatenate remaining parts of path; this will get shorter on each loop
            $test_path = implode('/', $url_parts) . '.php';
    
            if ( file_exists($test_path) )
            {
                // We've found a controller! Look no further!
                return $test_path;
            }
            else
            {
                // No controller there. Let's remove the last path part and try again
                array_pop($url_parts);
            }
        }
    
        // If we reach here, we never found a controller. This is probably an error.
        throw new Controller_Not_Found_Exception();
    }
    

    EDIT (after accepted) As per hakra’s comment below, the files to be included would presumably always have the extension “.php”, so sample now reflects this.

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

Sidebar

Related Questions

I am working with php and want to have a page that has a
I have my own hand-rolled PHP MVC framework for some projects that I'm working
I am working on an MVC framework in PHP but I work with java
I'm learning JSP and have a good working knowledge of PHP and MVC frameworks.
I have a directory structure in my Php mvc app that is currently like
So I've been working on an PHP MVC for a client for about 3
I'm working on a PHP web app and I'm trying to use the MVC
I'm working on a small MVC framework in PHP for an exercise. PHP, however,
We currently have a working php mail script, this works fine and as we
I'm working in PHP and I want to create a function that, given a

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.