Codeigniter Dynamic Routing

心已入冬 提交于 2019-12-06 09:27:37

I didn't thoroughly read your question, but this immediately caught my attention:

if (is_dir($path . '/' . $folder)) {
    echo "$route['$folder/(:any)'] = '$folder/index/$1';"; //<---- why echo ???
}

Honestly I'm not sure why this didn't cause serious issues for you in addition to not working.

You don't want to echo the route here, that will just try to print the string to screen, it's not even interpreted as PHP this way. There are also some issues with quotes that need to be remedied so the variables can be read as variables, not strings. Try this instead:

if (is_dir($path . '/' . $folder)) {
    $route[$folder.'/(:any)'] = $folder.'/index/$1';
}

Aside: I'd like to offer some additional resources that are not directly related to your problem, but should help you nonetheless with a solution:

It's hard to say why the registering of your routes fails. From your code I can see that you're not registering the routes (you just echo them), additionally I see that the usage of variables in strings are used inconsistently. Probably you mixed this a bit, the codeigniter documentation about routes is not precise on it either (in some minor points, their code examples are not really syntactically correct, but overall it's good).

I suggest you first move the logic to register your dynamic routes into a function of it's own. This will keep things a bit more modular and you can more easily change things and you don't pollute the global namespace with variables.

Based on this, I've refactored your code a bit. It does not mean that this works (not tested), however it might make things more clear when you read it:

function register_dynamic_routes($path, array &$route)
{
    $nodes = scandir($path);
    if (false === $nodes)
    {
        throw new InvalidArgumentException(sprintf('Path parameter invalid. scandir("$path") failed.', $path));
    }

    foreach ($nodes as $node)
    {
        if ($node === '.' or $node === '..')
            continue
            ;

        if (!is_dir("{$path}/{$node}")
            continue
            ;

        $routeDef = "{$folder}/(:any)";
        $routeResolve = "{$folder}/index/\$1";
        $route[$routeDef] = $routeResolve;

        # FIXME debug output
        echo "\$route['{$routeDef}'] = '{$routeResolve}';";
    }
}

$path = APPPATH.'controllers/';
register_dynamic_routes($path, $route);
$route['(:any)'] = 'main/index/$1';

Next to this you probably might not want to shift everything onto the index action, but a dynamic action instead. Furthermore, you might want to have a base controller that is delegating everything into the sub-controllers instead of adding the routes per controller. But that's up to you. The example above is based on the directory approach you outlined in your question.

Edit: Additional information is available in the Controllers section next to the URI Routing section

All this seems kind of complicated.

Plus, if you have hundreds (or thousands or more?) of possible routes in a database you may not want to load all of them into the "$routes" array every time any page loads in your application.

Instead, why not just do this?

last line of routes.php:

$route['404_override'] = 'vanity';

Controller file: Vanity.php:

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');

class Vanity extends MY_Controller {

/**
 * Vanity Page controller.
 *
 */

public function __construct() {
    parent::__construct();
}


public function index()
{
    $url = $_SERVER['PHP_SELF'];
    $url = str_replace("/index.php/", "", $url);

    // you could check here if $url is valid. If not, then load 404 via:
    //
    //  show_404();
    //
    // or, if it is valid then load the appropriate view, redirect, or
    // whatever else it is you needed to do!


    echo "Hello from page " . $url;
    exit;



}


}


?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!