-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(app): Added controller attributes #9745
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
Merged
lonnieezell
merged 12 commits into
codeigniter4:4.7
from
lonnieezell:autoroute-attributes
Oct 8, 2025
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d34d9c1
feat(app): Added controller attributes
lonnieezell 7667989
chore(app): Fix test Group use statements
lonnieezell 963876f
build(app): Fixing style and sa issues
lonnieezell e84bed3
build(app): Fixing more analyses errors
lonnieezell eeee9af
build(app): Additional fixes
lonnieezell e65e4fd
CS fixes
lonnieezell d712444
feat(app): Add config setting to use controller attributes or not
lonnieezell dbad51e
code fixes
lonnieezell 907981a
Apply suggestions from code review
lonnieezell 58a9eba
qa fixes
lonnieezell a24f400
Replace md5 in cache attribute with xxh128
lonnieezell 38b5f3c
cs fix
lonnieezell 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,142 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* 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\Router\Attributes; | ||
|
||
use Attribute; | ||
use CodeIgniter\HTTP\RequestInterface; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
|
||
/** | ||
* Cache Attribute | ||
* | ||
* Caches the response of a controller method at the server level for a specified duration. | ||
* This is server-side caching to avoid expensive operations, not browser-level caching. | ||
* | ||
* Usage: | ||
* ```php | ||
* #[Cache(for: 3600)] // Cache for 1 hour | ||
* #[Cache(for: 300, key: 'custom_key')] // Cache with custom key | ||
* ``` | ||
* | ||
* Limitations: | ||
* - Only caches GET requests; POST, PUT, DELETE, and other methods are ignored | ||
* - Streaming responses or file downloads may not cache properly | ||
* - Cache key includes HTTP method, path, query string, and possibly user_id(), but not request headers | ||
* - Does not automatically invalidate related cache entries | ||
* - Cookies set in the response are cached and reused for all subsequent requests | ||
* - Large responses may impact cache storage performance | ||
* - Browser Cache-Control headers do not affect server-side caching behavior | ||
* | ||
* Security Considerations: | ||
* - Ensure cache backend is properly secured and not accessible publicly | ||
* - Be aware that authorization checks happen before cache lookup | ||
*/ | ||
#[Attribute(Attribute::TARGET_METHOD)] | ||
class Cache implements RouteAttributeInterface | ||
{ | ||
public function __construct( | ||
public int $for = 3600, | ||
public ?string $key = null, | ||
) { | ||
} | ||
|
||
public function before(RequestInterface $request): RequestInterface|ResponseInterface|null | ||
{ | ||
// Only cache GET requests | ||
if ($request->getMethod() !== 'GET') { | ||
return null; | ||
} | ||
|
||
// Check cache before controller execution | ||
$cacheKey = $this->key ?? $this->generateCacheKey($request); | ||
|
||
$cached = cache($cacheKey); | ||
// Validate cached data structure | ||
if ($cached !== null && (is_array($cached) && isset($cached['body'], $cached['headers'], $cached['status']))) { | ||
$response = service('response'); | ||
$response->setBody($cached['body']); | ||
$response->setStatusCode($cached['status']); | ||
// Mark response as served from cache to prevent re-caching | ||
$response->setHeader('X-Cached-Response', 'true'); | ||
|
||
// Restore headers from cached array of header name => value strings | ||
foreach ($cached['headers'] as $name => $value) { | ||
$response->setHeader($name, $value); | ||
} | ||
$response->setHeader('Age', (string) (time() - ($cached['timestamp'] ?? time()))); | ||
|
||
return $response; | ||
} | ||
|
||
return null; // Continue to controller | ||
} | ||
|
||
public function after(RequestInterface $request, ResponseInterface $response): ?ResponseInterface | ||
{ | ||
// Don't re-cache if response was already served from cache | ||
if ($response->hasHeader('X-Cached-Response')) { | ||
// Remove the marker header before sending response | ||
$response->removeHeader('X-Cached-Response'); | ||
|
||
return null; | ||
} | ||
|
||
// Only cache GET requests | ||
if ($request->getMethod() !== 'GET') { | ||
return null; | ||
} | ||
|
||
$cacheKey = $this->key ?? $this->generateCacheKey($request); | ||
|
||
// Convert Header objects to strings for caching | ||
$headers = []; | ||
|
||
foreach ($response->headers() as $name => $header) { | ||
// Handle both single Header and array of Headers | ||
if (is_array($header)) { | ||
// Multiple headers with same name | ||
$values = []; | ||
|
||
foreach ($header as $h) { | ||
$values[] = $h->getValueLine(); | ||
} | ||
$headers[$name] = implode(', ', $values); | ||
} else { | ||
// Single header | ||
$headers[$name] = $header->getValueLine(); | ||
} | ||
} | ||
|
||
$data = [ | ||
'body' => $response->getBody(), | ||
'headers' => $headers, | ||
'status' => $response->getStatusCode(), | ||
'timestamp' => time(), | ||
]; | ||
|
||
cache()->save($cacheKey, $data, $this->for); | ||
|
||
return $response; | ||
} | ||
|
||
protected function generateCacheKey(RequestInterface $request): string | ||
{ | ||
return 'route_cache_' . md5( | ||
$request->getMethod() . | ||
$request->getUri()->getPath() . | ||
$request->getUri()->getQuery() . | ||
(function_exists('user_id') ? user_id() : ''), | ||
); | ||
} | ||
} |
This file contains hidden or 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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* 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\Router\Attributes; | ||
|
||
use Attribute; | ||
use CodeIgniter\HTTP\RequestInterface; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
|
||
/** | ||
* Filter Attribute | ||
* | ||
* Applies CodeIgniter filters to controller classes or methods. Filters can perform | ||
* operations before or after controller execution, such as authentication, CSRF protection, | ||
* rate limiting, or request/response manipulation. | ||
* | ||
* Limitations: | ||
* - Filter must be registered in Config\Filters.php or won't be found | ||
* - Does not validate filter existence at attribute definition time | ||
* - Cannot conditionally apply filters based on runtime conditions | ||
* - Class-level filters cannot be overridden or disabled for specific methods | ||
* | ||
* Security Considerations: | ||
* - Filters run in the order specified; authentication should typically come first | ||
* - Don't rely solely on filters for critical security; validate in controllers too | ||
* - Ensure sensitive filters are registered as globals if they should apply site-wide | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
class Filter implements RouteAttributeInterface | ||
{ | ||
public function __construct( | ||
public string $by, | ||
public array $having = [], | ||
) { | ||
} | ||
|
||
public function before(RequestInterface $request): RequestInterface|ResponseInterface|null | ||
{ | ||
// Filters are handled by the filter system via getFilters() | ||
// No processing needed here | ||
return null; | ||
} | ||
|
||
public function after(RequestInterface $request, ResponseInterface $response): ?ResponseInterface | ||
{ | ||
return null; | ||
} | ||
|
||
public function getFilters(): array | ||
{ | ||
if ($this->having === []) { | ||
return [$this->by]; | ||
} | ||
|
||
return [$this->by => $this->having]; | ||
} | ||
} |
Oops, something went wrong.
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.
This can use xxhash 128
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.
That's a valid point.
hash('xxh128')
is much faster thanmd5()
and fits great for cache key generation.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.
Good point! Changed.