Welcome Guest, Not a member yet? Register   Sign In
Modular Separation - PHP5 (Modules)

[eluser]Muser[/eluser]
@Buso

I am using wiredesignz Widget to do view partials.

I think it isn't a good solution as I would like, but it works.

Like you, I think it would be nice an update to ME, because intelligent view partials in modules it is an interesting solution. Are there any other HMVC libraries?

[eluser]Muser[/eluser]
To clarify my last response, I use Modular Separation and Widget together

[eluser]Ripe[/eluser]
[quote author="Jelmer" date="1277446618"]@Ripe

I've done that by adding the following to the main routes.php config file:
Code:
// Check if first segment corresponds to a module
$segment = substr(load_class('URI')->_parse_request_uri(), 1);
$segment = str_replace(load_class('Config')->item('url_suffix'), '', $segment);
$segment = array_shift(explode('/', $segment));
if ( ! empty($segment) && is_dir(APPPATH.'modules/'.$segment))
    $route[$segment.'(:any)?'] = $segment.'$1';
Which finds the first segment in the $segment variable and only adds the rule when a module of that name exists. So you still need the "local" routes.php files, but the modules will be drag-and-drop.

This could probably be done in a way that's better from a logical point of view (there shouldn't be any programming in the config files), but this works like a charm and works without having to make any changes to the Modular Seperation libs.

EDIT (4 July 2010): I just updated to CI2.0 and that needs a small change to the code above:
Code:
// Check if first segment corresponds to a module
$segment = substr(load_class('URI', 'core')->_parse_request_uri(), 1);
$segment = str_replace(load_class('Config')->item('url_suffix'), '', $segment);
$segment = array_shift(explode('/', $segment));
if ( ! empty($segment) && is_dir(APPPATH.'modules/'.$segment))
    $route[$segment.'(:any)?'] = $segment.'$1';
The load_class() function needs to know that URI is a core library or it will throw an error.[/quote]

Not sure why but this didn't work for me, I ended up making a small modification to MY_Router.php,

Code:
/* use module route if available */
if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
    $segments = $routes;
} else {
    if ($dh = opendir(APPPATH.'modules/')) {
        while ($file = readdir($dh)) {
            if ($file != "." && $file != "..") {
                if ($routes = Modules::parse_routes($file, implode('/', $segments))) {
                    $segments = $routes;
                }
            }
        }
        closedir($dh);
    }
}

Not sure if there are any flaws yet, but this will load the routes in all modules without the module name in the URI. Basically all it does it save you from modifying the main routes file in application/config/routes.php.

If anyone has a better way, please let me know! I never feel like what I do is the proper way...

[eluser]Jelmer[/eluser]
Quote:Not sure why but this didn’t work for me
Weird that it wouldn't work. There was still one small mistake in there, but it would work anyway without the correction. load_class('Config') also should have the second 'core' parameter in CI2, but as Config is already loaded at that point it wasn't a problem.

Quote:Not sure if there are any flaws yet, but this will load the routes in all modules without the module name in the URI.
I think you're trying to do something different then I wanted. I do want the module name in the URI, I just want them recognized on drag-and-dropping a module in there while still having $route[':any'] in there. If I understand it correctly with your solution you're actually making each module controller directory an extention of the normal application controller directory.
The downside, in my view, is that you're loading all routes files even when they're not needed - which adds unneccessary overhead, especially if you got a lot of modules. And controllers with the same name in different modules will conflict I think?

Quote:Basically all it does it save you from modifying the main routes file in application/config/routes.php.
It saves you from adding programming logic to a config file. But other than that there's never anything wrong wit "modifying the main routes file" as that's a config file that should be modified to suit your needs.

In the end we seem to have different goals & solutions. Yours making each module an extention of the main application without URI difference. While mine allows the use of $route[':any'] and still drag-and-dropping modules that will have their name in the URI.

[eluser]Ripe[/eluser]
@Jelmer
I didn't notice any difference to the speed but I only have a few modules at the moment so it probably will decrease eventually. And yes controllers are overwritten. I'm actually rethinking the usefulness of my solution because even if I did wanted to drop-n-drag modules into other applications I would still have to copy the module separation files so what's the harm in moving the routes.php file too. So I'm probably going to end up forgetting the whole thing lol.

[eluser]Prabhjeet[/eluser]
Ok, I have one problem in local routing.
I have a pages module, in url i can see each pages dynamically as following:
Code:
http://localhost/ci_cms/pages/index/portfolio

and to make it shorten as
Code:
http://localhost/ci_cms/p/portfolio
i have added this in main global route file,
Code:
$route['p/:any'] = "pages/index/:any";
, but if I add this route code in Page module's local config file, it gives me error that
Code:
404 Page Not Found
The page you requested was not found.

Then again if I put
Code:
$route['pages/:any'] = "pages/index/:any";
in my local route file, then i can see pages in url as
Code:
http://localhost/ci_cms/pages/portfolio

But following shorten version not working, why ?
Code:
$route['p/:any'] = "pages/index/:any";


Any solution to add it in local route file ?

Thanks

[eluser]Buso[/eluser]
@Ripe
@Jelmer

I see what Ripe wanted. Sounds good but as Jelmer said there might be conflicts between modules. Maybe it could work if we could turn it on/off, but that's too much for my brain at the moment.

What I don't get is what jelmer's code do. I'm using CI 2.0 and MS modules seem to be perfectly drag and droppable. What do you mean with $route[:any]? I'm using that in the module's routes.php, but I didn't have to add any code to make it work.

[eluser]Jelmer[/eluser]
[quote author="Buso" date="1278328852"]What I don't get is what jelmer's code do. I'm using CI 2.0 and MS modules seem to be perfectly drag and droppable. What do you mean with $route[:any]? I'm using that in the module's routes.php, but I didn't have to add any code to make it work.[/quote]
The $route[':any'] is in my main routes.php - everything is being handled by one controller unless there's a corresponding (module-)controller for the URI.

[eluser]Buso[/eluser]
Oh I see.. So you are using DB routes, right? Unless the first segment matches a module name.

[eluser]Jelmer[/eluser]
No, no DB routing. But everything that isn't handled by a controller is assumed to be just a normal page on the website and send to the Pages controller.
The code I posted checks whether the current first segment matches a module and adds the exception to the :any rule when a module was found. So such a request is indeed handled by the module-controller.




Theme © iAndrew 2016 - Forum software by © MyBB