-3

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" 

*/
Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53
  • I'm not clear what you are trying to achieve here. What is the actual input to this section of code? Are you trying to find the file to load based on a class name? Or based on a URL? Why do you need to test multiple possibilities? – IMSoP Aug 12 '12 at 14:30
  • I'm done with including controller class, find a class name end etc.. But, in controller folder I want create inside controller folder file inclusion of controller file. Example if doesn't file exists in controller folder then should search inside folder called by URI http://uri1/uri2/uri3 uri1 didn't file controller, but uri2 is found inside folder controller than uri2 is controller file. – Marin Sagovac Aug 12 '12 at 14:36
  • Example of this, but NOT on ROUTING mode, should be including and checking controller file before is called > http://stackoverflow.com/questions/6529026/codeigniter-default-controller-in-a-sub-directory-not-working – Marin Sagovac Aug 12 '12 at 14:39
  • so your function would take URL as input and file name as output? – IMSoP Aug 12 '12 at 15:29
  • Yes. Input is $uri_segments as array exploded by "/". If exists file than called as controller, but if didn't exists than should check inside folder for controller file. Above example works, but I'm thinking should be optimised and better method, excluding routing. – Marin Sagovac Aug 12 '12 at 15:35

1 Answers1

-1

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.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Also, i'm negative surprise of downvote... it's fake downvote! – Marin Sagovac Aug 13 '12 at 14:56
  • 1
    I can not explain the downvotes, but your PHP files the controllers are in should have the `.php` file extension or similar. In this case it's not the case which is just fishy. Also be aware that some malicious data can be injected via URIs, not all webservers do a normalization of it. So this can actually be a risky thing. – hakre Aug 13 '12 at 15:47
  • Both very good points. Adding ".php" is a trivial matter, so I've added it to my sample code. Escaping/normalisation is a little more involved, but I agree that there should probably be some within this function (never trust input, and all that). I suppose you could use a regex to test `$url_path` for non-alphanumerics, for a start. – IMSoP Aug 13 '12 at 15:55
  • Yes, but i'm needing a part of code, not of how include file, how include extension, etc... i'm using EX constant which called '.php' extension, because script may be using with php5 or something else... About malicous data i'm also using pre-loading class for xss and uri sanitisation prevention, so this isn't in question. Question is optimise code convention. I need some part of code not all. And using without .php extension in uri bacause i'm using on .htaccess policy without showing extension on URI. Thanks. – Marin Sagovac Aug 13 '12 at 15:57