New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can modules define their own route in Yii 2 #810

Closed
ymodulus opened this Issue Aug 25, 2013 · 17 comments

Comments

Projects
None yet
8 participants
@ymodulus

ymodulus commented Aug 25, 2013

digging into the core module file i cant find any where that enable a module define its own route. I left yii 1 to symfony2 bcuz i cudnt wait for Yii2 now i have been tracking the progress here and i seem to find it odd that in Yii 2 modules cannot define their own routes. Or is there any work around this?

@lucianobaraglia

This comment has been minimized.

Show comment
Hide comment
@lucianobaraglia

lucianobaraglia Aug 25, 2013

Contributor

Can't you define your custom rules in the configuration file?

Contributor

lucianobaraglia commented Aug 25, 2013

Can't you define your custom rules in the configuration file?

@ymodulus

This comment has been minimized.

Show comment
Hide comment
@ymodulus

ymodulus Aug 25, 2013

this is what i mean.

Can modules have their own configuration file where you define routes without touching the main app config file?
E.g in Symfony , Laravel 4 and django, bundles or modules or package have a route.yaml or watever file in this file you cam define routes without editing the main config file. This mean when you add a module. All routes are added automatically. And when you remove a module all route are remove automatically. This allows for high decoupling. But from what i can see yii 2 have a flaw in its config.

However is this possible to achieve?

ymodulus commented Aug 25, 2013

this is what i mean.

Can modules have their own configuration file where you define routes without touching the main app config file?
E.g in Symfony , Laravel 4 and django, bundles or modules or package have a route.yaml or watever file in this file you cam define routes without editing the main config file. This mean when you add a module. All routes are added automatically. And when you remove a module all route are remove automatically. This allows for high decoupling. But from what i can see yii 2 have a flaw in its config.

However is this possible to achieve?

@bwoester

This comment has been minimized.

Show comment
Hide comment
@bwoester

bwoester Aug 25, 2013

Contributor

Maybe this can be done using the extension init script (see #781)?

Contributor

bwoester commented Aug 25, 2013

Maybe this can be done using the extension init script (see #781)?

@rawtaz

This comment has been minimized.

Show comment
Hide comment
@rawtaz

rawtaz Aug 25, 2013

Contributor

I can attest that this has been asked for every now and then and I think it makes sense. People want their modules to be decoupled and not have to add route info for them into the config file or similar. I'm not sure if it can be done by with some init()-like stuff in the module class file (e.g. adding routes to the url manager dynamically), perhaps?

Even if it's doable it might not be straight-forward to know how a module should add its rules, i mean in which order. It could add its rules at the top of the list but this can still be problematic due to the nature of url rules, that the most explicit ones need to be first, etc.

Contributor

rawtaz commented Aug 25, 2013

I can attest that this has been asked for every now and then and I think it makes sense. People want their modules to be decoupled and not have to add route info for them into the config file or similar. I'm not sure if it can be done by with some init()-like stuff in the module class file (e.g. adding routes to the url manager dynamically), perhaps?

Even if it's doable it might not be straight-forward to know how a module should add its rules, i mean in which order. It could add its rules at the top of the list but this can still be problematic due to the nature of url rules, that the most explicit ones need to be first, etc.

@bwoester

This comment has been minimized.

Show comment
Hide comment
@bwoester

bwoester Aug 25, 2013

Contributor

Dynamically adding module rules in the module's init method won't work, because yii first needs to parse the rules, then it can access the requested module. So there needs to be something at the application scope to register the rules.

Contributor

bwoester commented Aug 25, 2013

Dynamically adding module rules in the module's init method won't work, because yii first needs to parse the rules, then it can access the requested module. So there needs to be something at the application scope to register the rules.

@rawtaz

This comment has been minimized.

Show comment
Hide comment
@rawtaz

rawtaz Aug 25, 2013

Contributor

@bwoester That is probably true. What I was thinking about was some way such that Yii doesn't have to dig into every single module and parse their url rules to know where to route, but can load whatever url rules there are for the module that was requested (assuming there was a module that matched the requested route, e.g. there's a "foo" module and the route was /foo/bar/star, even if there's an url rule in the main app that matches that route - i.e. modules take precedence).

That way module url rules could be used only when the module is to be used, and the use of the url rules in it would then be to match the remainder of the requested route in/under the context of the module's "base route", i.e. the "foo" module's url routes would be used to match the /bar/star part of the example route above.

This sounds a bit complex, but maybe it isn't after all. One thing that comes to mind is that since in this case the module names are always "used" when a route matches them, one might want to be able to define a kind of "module alias" similar to how one define url rules. E.g. if I have the module "powerBoard" (some third party bulletin board module) I might not want the routes to contain that name but instead just "forum". I would then want to define an alias for the "powerBoard" module so that it is available via /forum instead of /powerBoard. This could be done in the main application config (or if the powerBoard module is part of another module, e.g. /foo/powerBoard, then it should be placed in the config for the foo module). Either case, perhaps an 'alias'=>'forum' in the 'modules' array would be the place for this.

Neither of what I wrote above addresses a way to let modules and/or sub-modules to add url rules that match the entire requested route, but as I can't really see how it would be possible to juggle multiple modules and get their various "full route matching" url rules to work with each other (i.e. the right ones taking the right precedence), perhaps what I describe above is a better solution.

Contributor

rawtaz commented Aug 25, 2013

@bwoester That is probably true. What I was thinking about was some way such that Yii doesn't have to dig into every single module and parse their url rules to know where to route, but can load whatever url rules there are for the module that was requested (assuming there was a module that matched the requested route, e.g. there's a "foo" module and the route was /foo/bar/star, even if there's an url rule in the main app that matches that route - i.e. modules take precedence).

That way module url rules could be used only when the module is to be used, and the use of the url rules in it would then be to match the remainder of the requested route in/under the context of the module's "base route", i.e. the "foo" module's url routes would be used to match the /bar/star part of the example route above.

This sounds a bit complex, but maybe it isn't after all. One thing that comes to mind is that since in this case the module names are always "used" when a route matches them, one might want to be able to define a kind of "module alias" similar to how one define url rules. E.g. if I have the module "powerBoard" (some third party bulletin board module) I might not want the routes to contain that name but instead just "forum". I would then want to define an alias for the "powerBoard" module so that it is available via /forum instead of /powerBoard. This could be done in the main application config (or if the powerBoard module is part of another module, e.g. /foo/powerBoard, then it should be placed in the config for the foo module). Either case, perhaps an 'alias'=>'forum' in the 'modules' array would be the place for this.

Neither of what I wrote above addresses a way to let modules and/or sub-modules to add url rules that match the entire requested route, but as I can't really see how it would be possible to juggle multiple modules and get their various "full route matching" url rules to work with each other (i.e. the right ones taking the right precedence), perhaps what I describe above is a better solution.

@ymodulus

This comment has been minimized.

Show comment
Hide comment
@ymodulus

ymodulus Aug 25, 2013

@rawtaz adding the route must not necessarily be dynamically. Take a look at symfony2 routing just see the doc. U wud get what i mean. Or take a look at Laravel 4 packages routing.

Also if you take a look at them you would notice that package or module or bundle can have their config file whil can overwrite the main application config. Also when you remove the bundle your config is removed.
This because their modules are designed to be highly decoupled.

And this is one great flaw yii has. Module are not really decoupled as they claim to be. I thought Qiang and the Yii team would address this issue in Yii 2 but they didnt.

This is also an issue i dont really favour pure arrays as config file.
Goshhhhhhhh!!!!!!!!

Angry.

ymodulus commented Aug 25, 2013

@rawtaz adding the route must not necessarily be dynamically. Take a look at symfony2 routing just see the doc. U wud get what i mean. Or take a look at Laravel 4 packages routing.

Also if you take a look at them you would notice that package or module or bundle can have their config file whil can overwrite the main application config. Also when you remove the bundle your config is removed.
This because their modules are designed to be highly decoupled.

And this is one great flaw yii has. Module are not really decoupled as they claim to be. I thought Qiang and the Yii team would address this issue in Yii 2 but they didnt.

This is also an issue i dont really favour pure arrays as config file.
Goshhhhhhhh!!!!!!!!

Angry.

@ymodulus

This comment has been minimized.

Show comment
Hide comment
@ymodulus

ymodulus Aug 25, 2013

@rawtaz too add to your point. Not all module like to follow the conventional routin (module/controller/action).

See if i have a user module and a auth controller with login method. By default yii. My url will be (user/auth/login) but i may want my url to be (/auth/login) or just (/login).

Another example is a page module.
Instead of Page/AboutUs. I may simply need /aboutus.

Note* now to do that in Yii 1 u have to edit main config file. But in other modern frameworks. (Symfony, Laravel, Django, Web2py) this is not the case. Modules can define url.
So if yii does not have this it is a deal breaker for me. Cuz yii modules are not interactive in api.

ymodulus commented Aug 25, 2013

@rawtaz too add to your point. Not all module like to follow the conventional routin (module/controller/action).

See if i have a user module and a auth controller with login method. By default yii. My url will be (user/auth/login) but i may want my url to be (/auth/login) or just (/login).

Another example is a page module.
Instead of Page/AboutUs. I may simply need /aboutus.

Note* now to do that in Yii 1 u have to edit main config file. But in other modern frameworks. (Symfony, Laravel, Django, Web2py) this is not the case. Modules can define url.
So if yii does not have this it is a deal breaker for me. Cuz yii modules are not interactive in api.

@rawtaz

This comment has been minimized.

Show comment
Hide comment
@rawtaz

rawtaz Aug 25, 2013

Contributor

@ymodulus From what I can tell by http://symfony.com/doc/current/book/routing.html#including-external-routing-resources you configure the url rules in Symfony2 to include those from e.g. a bundle/module. Looks like that, in combination with the prefix feature (also described on that URL) is effectively what I meant in my post to @bwoester above. Except that in the Symfony2 example the prefixing is explicit while in my suggestion explicit (though configurable with the alias of the module), and that the main config includes the module's url routes rather than the module's url routes only being used automatically when the module was requested (based in the left-most part(s) of the url). Without this prefix stuff I don't see how one can practically avoid precedence/collision issues.

@ymodulus Yes, I see what you mean in your second post to me. But this only works when your module's route does not conflict with the other modules or url rules in your system. If you have another url rule that is meant to catch /<\w> (this one is at the bottom of your main url rules) and then you want to add the "powerForum" module providing /forum, but you also want to add an "extendedForumProfile" module that is an addition to the powerForum one, and this module should provide /forum/profile (overriding the powerModule's /forum/profile, then you need to make sure that you "include" these modules/their url rules in the right order so that the latter one takes precedence. I just have a gut feeling that this type of juggling can become troublesome as you add various modules. Just look at how one must take care with main url rules already, to make sure that the right controllers/actions/whatever match the right routes. It won't get any easier when in your main config you can only reference various modules' url rules for full inclusion.

Perhaps in the end, setting up url rules in a completely controlled way in one single place is the way to go after all (assuming one doesn't want the implicit prefixing stuff I wrote to about to @bwoester - it doesn't take away the ability to write everything in the main url rules if that's what you prefer).

Contributor

rawtaz commented Aug 25, 2013

@ymodulus From what I can tell by http://symfony.com/doc/current/book/routing.html#including-external-routing-resources you configure the url rules in Symfony2 to include those from e.g. a bundle/module. Looks like that, in combination with the prefix feature (also described on that URL) is effectively what I meant in my post to @bwoester above. Except that in the Symfony2 example the prefixing is explicit while in my suggestion explicit (though configurable with the alias of the module), and that the main config includes the module's url routes rather than the module's url routes only being used automatically when the module was requested (based in the left-most part(s) of the url). Without this prefix stuff I don't see how one can practically avoid precedence/collision issues.

@ymodulus Yes, I see what you mean in your second post to me. But this only works when your module's route does not conflict with the other modules or url rules in your system. If you have another url rule that is meant to catch /<\w> (this one is at the bottom of your main url rules) and then you want to add the "powerForum" module providing /forum, but you also want to add an "extendedForumProfile" module that is an addition to the powerForum one, and this module should provide /forum/profile (overriding the powerModule's /forum/profile, then you need to make sure that you "include" these modules/their url rules in the right order so that the latter one takes precedence. I just have a gut feeling that this type of juggling can become troublesome as you add various modules. Just look at how one must take care with main url rules already, to make sure that the right controllers/actions/whatever match the right routes. It won't get any easier when in your main config you can only reference various modules' url rules for full inclusion.

Perhaps in the end, setting up url rules in a completely controlled way in one single place is the way to go after all (assuming one doesn't want the implicit prefixing stuff I wrote to about to @bwoester - it doesn't take away the ability to write everything in the main url rules if that's what you prefer).

@bwoester

This comment has been minimized.

Show comment
Hide comment
@bwoester

bwoester Aug 25, 2013

Contributor

@rawtaz: totally agree. Especially once there are many rules registered from various different modules, it would be good if they would be parsed incrementally instead of running over the whole list on every request.

Contributor

bwoester commented Aug 25, 2013

@rawtaz: totally agree. Especially once there are many rules registered from various different modules, it would be good if they would be parsed incrementally instead of running over the whole list on every request.

@ymodulus

This comment has been minimized.

Show comment
Hide comment
@ymodulus

ymodulus Aug 25, 2013

hmmmmm. Ok. @both i have nothing to say than agree. Keep my fingers crossed.

Thanks i appreciate

ymodulus commented Aug 25, 2013

hmmmmm. Ok. @both i have nothing to say than agree. Keep my fingers crossed.

Thanks i appreciate

@qiangxue

This comment has been minimized.

Show comment
Hide comment
Member

qiangxue commented Aug 25, 2013

@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Mar 21, 2014

Member

We now have bootstrap classes: #2796
This can be used to register the needed URL rules by extensions and modules.

Member

qiangxue commented Mar 21, 2014

We now have bootstrap classes: #2796
This can be used to register the needed URL rules by extensions and modules.

@cebe

This comment has been minimized.

Show comment
Hide comment
@cebe

cebe Mar 21, 2014

Member

Imo this fixes the whole issue.

Member

cebe commented Mar 21, 2014

Imo this fixes the whole issue.

@qiangxue

This comment has been minimized.

Show comment
Hide comment
@qiangxue

qiangxue Mar 22, 2014

Member

Added PrefixUrlRule: 1a71c10

This feature is now complete.

Member

qiangxue commented Mar 22, 2014

Added PrefixUrlRule: 1a71c10

This feature is now complete.

@qiangxue qiangxue closed this Mar 22, 2014

@terrasoff

This comment has been minimized.

Show comment
Hide comment
@terrasoff

terrasoff May 20, 2014

it's very comfortable adding routes in module config (like ZF2 or SF2 does it).
Also router's console commands are helpful tool.
Could we have same abilities in Yii2?

terrasoff commented May 20, 2014

it's very comfortable adding routes in module config (like ZF2 or SF2 does it).
Also router's console commands are helpful tool.
Could we have same abilities in Yii2?

@kimsreng

This comment has been minimized.

Show comment
Hide comment
@kimsreng

kimsreng Jun 5, 2014

it's cool to have that feature.

kimsreng commented Jun 5, 2014

it's cool to have that feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment