-
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
Merged
+347
−82
Merged
Clean router config #7380
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e49decb
wip
lonnieezell 481c2f9
refactor: Clean up routes file to be just for routes. Moved route set…
lonnieezell 31138ab
Fixing style issues
lonnieezell e37c2c6
More style fixes
lonnieezell ec0da19
Allow specifying the location of Routing files within modules.
lonnieezell 520a0a2
Change user guide changes to 4.4
lonnieezell 0604b40
Move routes files back to Config folder for now, and drop the modules…
lonnieezell b2ae0cc
Code quality improvements
lonnieezell 79bc661
Updating doc code samples and CodeIgniter test
lonnieezell f673df4
Apply suggestions from code review
lonnieezell 56eb702
Apply suggestions from code review
lonnieezell 7a0d1e4
Apply suggestions from code review
lonnieezell 35f9a1d
test: update failed tests
kenjis d74967f
test: fix incorrect tests
kenjis 1aba5db
docs: to pass cs-fix
kenjis 2eb2ee5
test: extract method
kenjis a47b8f1
docs: improve sample code class files
kenjis 329d90b
docs: add note for new config file
kenjis 5243ba3
docs: add changelog
kenjis e5e6766
docs: update ugrade_440
kenjis 48e3864
docs: add instruction to upgrade
kenjis 64130a2
fix: log message
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,8 @@ | ||
<?php | ||
|
||
namespace Config; | ||
use CodeIgniter\Router\RouteCollection; | ||
|
||
// Create a new instance of our RouteCollection class. | ||
$routes = Services::routes(); | ||
|
||
/* | ||
* -------------------------------------------------------------------- | ||
* Router Setup | ||
* -------------------------------------------------------------------- | ||
*/ | ||
$routes->setDefaultNamespace('App\Controllers'); | ||
$routes->setDefaultController('Home'); | ||
$routes->setDefaultMethod('index'); | ||
$routes->setTranslateURIDashes(false); | ||
$routes->set404Override(); | ||
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps | ||
// where controller filters or CSRF protection are bypassed. | ||
// If you don't want to define all routes, please use the Auto Routing (Improved). | ||
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true. | ||
// $routes->setAutoRoute(false); | ||
|
||
/* | ||
* -------------------------------------------------------------------- | ||
* Route Definitions | ||
* -------------------------------------------------------------------- | ||
/** | ||
* @var RouteCollection $routes | ||
*/ | ||
|
||
// We get a performance increase by specifying the default | ||
// route since we don't have to scan directories. | ||
$routes->get('/', 'Home::index'); | ||
|
||
/* | ||
* -------------------------------------------------------------------- | ||
* Additional Routing | ||
* -------------------------------------------------------------------- | ||
* | ||
* There will often be times that you need additional routing and you | ||
* need it to be able to override any defaults in this file. Environment | ||
* based routes is one such time. require() additional route files here | ||
* to make that happen. | ||
* | ||
* You will have access to the $routes object within that file without | ||
* needing to reload it. | ||
*/ | ||
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) { | ||
require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Config; | ||
|
||
use CodeIgniter\Config\Routing as BaseRouting; | ||
|
||
/** | ||
* Routing configuration | ||
*/ | ||
class Routing extends BaseRouting | ||
{ | ||
/** | ||
* An array of files that contain route definitions. | ||
* Route files are read in order, with the first match | ||
* found taking precedence. | ||
* | ||
* Default: APPPATH . 'Config/Routes.php' | ||
*/ | ||
public array $routeFiles = [ | ||
APPPATH . 'Config/Routes.php', | ||
]; | ||
|
||
/** | ||
* The default namespace to use for Controllers when no other | ||
* namespace has been specified. | ||
* | ||
* Default: 'App\Controllers' | ||
*/ | ||
public string $defaultNamespace = 'App\Controllers'; | ||
|
||
/** | ||
* The default controller to use when no other controller has been | ||
* specified. | ||
* | ||
* Default: 'Home' | ||
*/ | ||
public string $defaultController = 'Home'; | ||
|
||
/** | ||
* The default method to call on the controller when no other | ||
* method has been set in the route. | ||
* | ||
* Default: 'index' | ||
*/ | ||
public string $defaultMethod = 'index'; | ||
|
||
/** | ||
* Whether to translate dashes in URIs to underscores. | ||
* Primarily useful when using the auto-routing. | ||
* | ||
* Default: false | ||
*/ | ||
public bool $translateURIDashes = false; | ||
|
||
/** | ||
* Sets the class/method that should be called if routing doesn't | ||
* find a match. It can be either a closure or the controller/method | ||
* name exactly like a route is defined: Users::index | ||
* | ||
* This setting is passed to the Router class and handled there. | ||
* | ||
* If you want to use a closure, you will have to set it in the | ||
* class constructor or the routes file by calling: | ||
* | ||
* $routes->set404Override(function() { | ||
* // Do something here | ||
* }); | ||
* | ||
* Example: | ||
* public $override404 = 'App\Errors::show404'; | ||
*/ | ||
public $override404; | ||
|
||
/** | ||
* If TRUE, the system will attempt to match the URI against | ||
* Controllers by matching each segment against folders/files | ||
* in APPPATH/Controllers, when a match wasn't found against | ||
* defined routes. | ||
* | ||
* If FALSE, will stop searching and do NO automatic routing. | ||
*/ | ||
public bool $autoRoute = false; | ||
|
||
/** | ||
* If TRUE, will enable the use of the 'prioritize' option | ||
* when defining routes. | ||
* | ||
* Default: false | ||
*/ | ||
public bool $prioritize = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Config; | ||
|
||
/** | ||
* Routing configuration | ||
*/ | ||
class Routing extends BaseConfig | ||
{ | ||
/** | ||
* An array of files that contain route definitions. | ||
* Route files are read in order, with the first match | ||
* found taking precedence. | ||
* | ||
* Default: APPPATH . 'Config/Routes.php' | ||
*/ | ||
public array $routeFiles = [ | ||
APPPATH . 'Routes.php', | ||
]; | ||
|
||
/** | ||
* The default namespace to use for Controllers when no other | ||
* namespace has been specified. | ||
* | ||
* Default: 'App\Controllers' | ||
*/ | ||
public string $defaultNamespace = 'App\Controllers'; | ||
|
||
/** | ||
* The default controller to use when no other controller has been | ||
* specified. | ||
* | ||
* Default: 'Home' | ||
*/ | ||
public string $defaultController = 'Home'; | ||
|
||
/** | ||
* The default method to call on the controller when no other | ||
* method has been set in the route. | ||
* | ||
* Default: 'index' | ||
*/ | ||
public string $defaultMethod = 'index'; | ||
|
||
/** | ||
* Whether to translate dashes in URIs to underscores. | ||
* Primarily useful when using the auto-routing. | ||
* | ||
* Default: false | ||
*/ | ||
public bool $translateURIDashes = false; | ||
|
||
/** | ||
* Sets the class/method that should be called if routing doesn't | ||
* find a match. It can be either a closure or the controller/method | ||
* name exactly like a route is defined: Users::index | ||
* | ||
* This setting is passed to the Router class and handled there. | ||
* | ||
* If you want to use a closure, you will have to set it in the | ||
* class constructor or the routes file by calling: | ||
* | ||
* $routes->set404Override(function() { | ||
* // Do something here | ||
* }); | ||
* | ||
* Example: | ||
* public $override404 = 'App\Errors::show404'; | ||
*/ | ||
public $override404; | ||
|
||
/** | ||
* If TRUE, the system will attempt to match the URI against | ||
* Controllers by matching each segment against folders/files | ||
* in APPPATH/Controllers, when a match wasn't found against | ||
* defined routes. | ||
* | ||
* If FALSE, will stop searching and do NO automatic routing. | ||
*/ | ||
public bool $autoRoute = false; | ||
|
||
/** | ||
* If TRUE, will enable the use of the 'prioritize' option | ||
* when defining routes. | ||
* | ||
* Default: false | ||
*/ | ||
public bool $prioritize = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you really need this breaking change?
What if using
config('Routing')
in the__construct()
?If you really need it, I recommend you use refactoring feature in IDE
for updating all test code.
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.
In the same way the module config is sent it, I sent the Routing config in, so - seems like that's the proper way to do it....
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.
I also think injection is the better way than
config()
, but changing the method signature breaks a lot of client code...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 would only break code for those that have extended the RouteCollection class, which I can't imagine is that many. If you think this particular BC is too much, we can scrap this and save it for 5. Seemed like a smaller BC possibility than a number of our other breaks, unless I'm completely missing something here.
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.
Okay, then go with injection.