-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Clean router config #7380
Clean router config #7380
Conversation
This is not a trivial change. It should go to @lonnieezell Don't use |
I don't think this PR is a good decision. About configs. Almost all configs used in class dependencies are used as DTO, overriding class properties. About routes. Also, the presence of two files associated with routing can cause confusion. |
What is the meaning of using the config-class? And I know you loves array config, but we have been using config-class already. |
This is a good point. |
Good point! I'll fix that up.
That's true, I could just remove those settings all together and document how to call the methods within the Routes file. I have two issues with that, though. First - the current Routes file isn't what I would consider a config file, honestly. It was left in that folder for purely historical reasons during the rewrite. Second - this is the first of a two-step process to make the Router more flexible, converting it to use Handlers for the 2 distinct routing systems we have, but expandable with dev-provided routers. I've heard devs searching for a way to get routes dynamically from the database for years, and this would allow them to easily handle that if they needed, as well as allow others to create other routing if they needed. Knowing this, we need a place to configure the handlers that are in use. This could be handled in the Services class, I guess, though that doesn't feel right when you look at other portions that use handlers, like the Cache, Session, or Database.
Good point. I missed that. I see three options:
Thoughts? |
@kenjis You're exaggerating when you say I love arrays. I believe that arrays are the most optimal format for configuration. A class has only one advantage - it can be specified as a dependency. Of course, in this version of the framework, changing classes to arrays is unacceptable, but I still hope that a discussion about switching to arrays is possible in the next version. If we talk about the use of classes, then in my opinion a very strange approach is used. // instead of
$this->a = $config->a;
$this->b = $config->b;
// better
$this->config = $config; Since the config is in dependencies, without it we will not be able to create an instance of the class. Then it makes no sense to duplicate class properties in the config and we can use the config directly. |
Precisely because they tried to make CI4 more like CI3 and used its code base, we are now in a lot of trouble.
If you are talking about flexibility, then it already exists. Services! Though I still think ServiceContainer and Dependency Autowiring would be the best solution. If the developer wants to change the behavior of the router, then he can replace the service with his own implementation. Take for example the Model. It serves arrays, stdClass and Entity. Everything seems to be fine, but when adding features, you have to service all return types. Which increases the code base and complicates maintenance.
I think a single configuration point should be used for modules. Something similar to Registrars and Laravel ServiceProvider. namespace Module;
class Config
{
public function boot()
{
// This is where we define routes, filters, and other available settings.
// Instead of separate files that repeat the names of the application configs.
}
} |
@iRedds $this->a = $config->a A Config class should be a dependency for one class. If it is used in more than one class, it is not good. $this->config = $config; I did it in #7255 Why don't you approve it? |
What is the main module? I feel |
@kenjis To be honest, I don't quite understand what happened. That is, in CI3 the session settings were in the main config and everything worked, but in CI4 it does not work. |
@iRedds That is because CI4 uses Config classes. In the current implementation, the Session class constructor requires Config\App object. If you want to use Session object in Config\App to set a property, It will be an infinite loop. This issue does not occur in array config because there is no instantiation. |
@kenjis Forbid using the constructor in configs. |
The original reason Narf and I discussed using classes as config files years ago was that it could be loaded instantly from anywhere. Obviously, the framework has expanded and the Factory class now handles that to ensure it's only ever loaded once for performance, so it might be valid to rethink for v5. But that's not relevant to this PR.
Interesting idea, but it feels more relevant to a plug-in system for a CMS or other app. This would warrant other discussion, but is a huge breaking change and not something we can handle within this PR.
I was hesitant to create a new directory for a single file. If we had logic around the handling of different specific route file types, like Laravel has bulit-in logic around
Their locations can all be specified within the config file. And the current one can be moved to join them just by changing its path in the config file.
To some extent I agree with this direction. The three problems I'm struggling with are:
|
Tell me, how is the single point for the configuration different from the current state? In the technical part. That is, in order for the framework to process these files, they must comply with certain rules. How is this different from a plug-in system for a CMS? You are talking about splitting routing handlers. For example, for predefined routes, there is no point in a default route. On the other hand, based on the experience of CI3, even with autorouting, it is necessary to be able to define routes manually. Is it possible to separate handlers in this case? |
Each route handler has its own settings.
In CI3 auto-routing is primary routing system, and there is no way to turn it off. But if you define all routes, you don't need to use auto-routing at all. |
Ok, so without re-inventing the core of the framework here, what are people leaning toward? Please think solutions that are consistent within the framework as it exists now. Or should we just close this PR? I'm fine with that if it's too controversial, though it would have been nice to get input from other core team members. |
I prefer go to 5.0 for this change, except, deprecate existing functionality to keep working while suggesting new feature. |
I'm a bit worried about the BC break part this PR brings when upgrading the framework, but overall I like the idea. Routes inside the modules should be auto-discovered as before, though I'm unsure how we should specify the default location after these changes. If we make a clear path of how to upgrade, I'm okay with including this in the v4.4 release. |
I would like to get this change into 4.4 with no breaking changes. |
e39151f
to
794c734
Compare
I've rebased to 4.4 branch. I also made the path within any "modules" configurable. This migration should be pretty painless - see the instructions I added to the PR description for details). |
Defaulting to Wouldn't it be better to leave the default settings under Config?
It doesn't seem worth breaking all the existing packages and changing to |
…Path config setting.
Co-authored-by: Michal Sniatala <[email protected]>
Co-authored-by: kenjis <[email protected]>
cad3378
to
1aba5db
Compare
Rebased and updated failed tests, and added documentation a bit. |
I think this is ready to merge. @codeigniter4/core-team |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems ready to me.
In error messages, we do not place a dot in the sentence when it comes `:` + word.
The router file had way too much information in it, from walls of comments with descriptions of settings, to settings, and including additional files. This PR cleans this up so that the Routes.php file is only for adding routes. It does this by:
app/Config/Routing.php
. All of the settings that were set by default within the oldRoutes
file has been moved to this config file.TheRoutes
file has been moved toapp/Routes.php
to make it more prominent, and reduce confusion by having two closely-named files.Routes.php
files within modules are now expected to be at the root of the module directory, though this is easily changed.Routing
config file allows existing applications to simply point to the oldRoutes
file if they don't want to move it. This also allows the user to specify multiple routes files to include if they want to separate routes logically, like into anapi
andweb
files, etc.A new setting within theRouting
config file allows setting the path to module config files.This now also defaults to{module}/Routes.php
.UPGRADING EXISTING PROJECTS:
The process for upgrading an existing project should be simple:
Routing.php
config file into the project folder.edit the$routeFiles
to point to the existing file (or move the existing file into theapp
folder)edit the$modulePath
to beConfig/Routes.php
(or move any existing files up to the module root folder)Checklist: