travy / cake-tracking
Request tracking middleware used for CakePHP 3 applications
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:cakephp-plugin
Requires
- cakephp/cakephp: ^3.4
Requires (Dev)
- phpunit/phpunit: ^5.7|^6.0
This package is not auto-updated.
Last update: 2024-05-15 19:22:24 UTC
README
Description
The Cake Tracking Plugin provides Middleware which will record all Request made to a web application. All logging can be customized per application but there is also a default logger which will write to the file system.
In addition, external IP addresses can be blacklisted from the site by adding the address to a blacklist file.
Installation
Add the package to your CakePHP projects composer.json
composer require travy/cake-tracking:1.0
Next, be sure to add the plugin to the config/bootstrap.php
file by executing the following command in the command prompt
bin/cake plugin load cake-tracking
Now the Middleware can be loaded into the application pipeline by appending it to the MiddlewareQueue found in `src/Application.php'
class Application extends BaseApplication { public function middleware($middlewareQueue) { $middlewareQueue ->add(ErrorHandlerMiddleware::class) ->add(AssetMiddleware::class) ->add(new RoutingMiddleware($this)) // add the TrackingMiddleware to the queue ->add(new \CakeTracking\Middleware\TrackingMiddleware()); return $middlewareQueue; } }
Finally, you should be able to add any custom configuration necessary to complete the environment.
If you want to use a custom Logger or blacklisting solution (perhaps to store data in a database)
then you just need to create a class which implements either TrackingLoggerInterface
or BlacklistRepositoryInterface
.
Then before adding the Middleware to the MiddlewareQueue, you can specify your custom classes
using the built in setter calls.
class Application extends BaseApplication { public function middleware($middlewareQueue) { // supply the custom classes to the TrackingMiddleware object $trackingMiddleware = new \CakeTracking\Middleware\TrackingMiddleware(); $trackingMiddleware->setBlacklistRepository(new BlacklistDatabaseRepository($configs)); $trackingMiddleware->setLoggingOperation(new TrackingDatabaseLoggin($configs)); $middlewareQueue ->add(ErrorHandlerMiddleware::class) ->add(AssetMiddleware::class) ->add(new RoutingMiddleware($this)) // supply the instantiated tracker to the queue ->add($trackingMiddleware); return $middlewareQueue; } }
Configurations
The following configurations are optional but will be useful for system setup.
In config/app.php
add the following:
'CakeTracking' => [ 'LogFile' => LOGS . 'bamboo.txt', 'Blacklist' => LOGS . 'blacklist.txt', ],
The LogFile configuration will specify where all Requests should be logged.
Blacklist specifies the location of the blacklist on the system.