nowo-tech / controller-kit-bundle
Controller utilities for Symfony: redirectToReferer (configurable default route) and SafeForwardTrait for safe request forwarding.
Package info
github.com/nowo-tech/ControllerKitBundle
Type:symfony-bundle
pkg:composer/nowo-tech/controller-kit-bundle
Requires
- php: >=8.2 <8.6
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/http-foundation: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
- symfony/routing: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.0
- phpstan/phpstan-symfony: ^2.0
- phpunit/phpunit: ^10.0
- rector/rector: ^2.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/yaml: ^6.4 || ^7.0 || ^8.0
Suggests
- symfony/flex: For automatic recipe installation
- symfony/framework-bundle: Required in your application when using the traits with AbstractController
This package is auto-updated.
Last update: 2026-03-23 17:04:55 UTC
README
Controller Kit Bundle — Utilities for Symfony controllers: redirectToReferer (configurable default route) and SafeForwardTrait for safe request forwarding. For Symfony 6.4, 7 and 8 · PHP 8.2+.
Features
- RedirectToRefererTrait — Redirect to the HTTP Referer when valid (same app, route exists), or to a configurable default route. 100% configurable via
nowo_controller_kit.default_route. - SafeForwardTrait — Forward to another controller method with a check that the method exists (avoids runtime errors).
Installation
composer require nowo-tech/controller-kit-bundle
With Symfony Flex, the recipe registers the bundle and adds config. Without Flex, see docs/INSTALLATION.md.
Manual registration in config/bundles.php:
return [ // ... Nowo\ControllerKitBundle\NowoControllerKitBundle::class => ['all' => true], ];
Configuration
In config/packages/nowo_controller_kit.yaml:
nowo_controller_kit: default_route: homepage # Route used when redirectToReferer has no valid Referer
Use your own route name (e.g. app_home, dashboard).
Usage
redirectToReferer
Use the trait in a controller that extends AbstractController:
use Nowo\ControllerKitBundle\Controller\RedirectToRefererTrait; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; class MyController extends AbstractController { use RedirectToRefererTrait; public function submit(Request $request): Response { // ... handle form ... return $this->redirectToReferer($request); // Optional: merge params and set status // return $this->redirectToReferer($request, ['success' => 1], 303); } }
When the request has a valid Referer header whose path matches a route in your app, the user is redirected there (with path and query params preserved). Otherwise they are redirected to the configured default_route.
safeForward
Use the trait in any controller that has forward() (e.g. AbstractController):
use Nowo\ControllerKitBundle\Controller\SafeForwardTrait; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ApiController extends AbstractController { use SafeForwardTrait; public function delegate(): Response { return $this->safeForward( OtherController::class, 'actionName', ['id' => 123], ['page' => 1] ); } }
If OtherController::actionName does not exist, a BadMethodCallException is thrown instead of a generic error.
Documentation
- Installation
- Configuration
- Usage
- Demo (FrankenPHP)
- Contributing
- Changelog
- Upgrading
- Release
- Security
Requirements
- PHP 8.2+
- Symfony 6.4 / 7.0 / 8.0
- For traits:
symfony/framework-bundlein your application (AbstractController)
Development
make up
make install
make test
make cs-check
make phpstan
make release-check
Demo
demo/symfony7(port8010)demo/symfony8(port8011)- Global demo commands:
make -C demo help
License and author
MIT · Nowo.tech · Héctor Franco Aceituno