jippi / cakephp-newrelic
该包已被废弃,不再维护。未建议替代包。
CakePHP <3 NewRelic
dev-master
2018-07-28 07:23 UTC
Requires
This package is auto-updated.
Last update: 2024-06-29 03:40:51 UTC
README
CakePHP <3 NewRelic
您可以这样修改您的文件
包含的内容
- NewRelic.NewRelic 任务
- NewRelic.NewRelic 组件
- NewRelicTrait 特性
- NewRelic.NewRelic
安装
composer require jippi/cakephp-newrelic
控制台
将以下代码片段包含在 app/Console/AppShell.php
public function startup() { $this->NewRelic = $this->Tasks->load('NewRelic.NewRelic'); $this->NewRelic->setName($this); $this->NewRelic->start(); $this->NewRelic->parameter('params', json_encode($this->params)); $this->NewRelic->parameter('args', json_encode($this->args)); parent::startup(); }
控制器
只需将 NewRelic.NewRelic
添加到您的 $components
列表
app/webroot/index.php
在文件顶部添加此代码,在 define('DS', 'DIRECTORY_SEPARATOR')
之前
<?php require_once dirname(dirname(__DIR__)) . '/vendors/autoload.php'; if (extension_loaded('newrelic')) { $appType = 'app'; $appName = 'web'; if (strpos($_SERVER['REQUEST_URI'], '/admin/') !== false) { $appName = 'admin'; } define('NEW_RELIC_APP_NAME', sprintf('%1$s - %2$s - %3$s', 'production', $appType, $appName)); newrelic_set_appname(NEW_RELIC_APP_NAME); newrelic_background_job(false); newrelic_capture_params(true); } // Rest of your index.php here
app/Console/cake.php
<?php require_once dirname(dirname(__DIR__)) . '/vendors/autoload.php'; if (extension_loaded('newrelic')) { define('NEW_RELIC_APP_NAME', sprintf('%s - app - cli', 'production')); newrelic_set_appname(NEW_RELIC_APP_NAME); newrelic_background_job(true); newrelic_capture_params(true); } // Rest of your cake.php file here
如果使用 > CakePHP 3.3.0 并使用中间件,请注意
如果您使用来自 https://book.cakephp.com.cn/3.0/en/controllers/middleware.html 的 CakePHP 中间件
您可以使用提供的 NewRelicErrorHandlerMiddleware
,它位于 NewRelic\Middleware\NewRelicErrorHandlerMiddleware
中,并扩展了内置的 Cake\Error\Middleware\ErrorHandlerMiddleware
。通过使用它,您将获得 NewRelic 的功能,并且还有默认的 CakePHP 行为。
示例
<?php namespace App; use Cake\Http\BaseApplication; use Cake\Routing\Middleware\AssetMiddleware; use Cake\Routing\Middleware\RoutingMiddleware; /** * Application setup class. * * This defines the bootstrapping logic and middleware layers you * want to use in your application. */ class Application extends BaseApplication { /** * Setup the middleware your application will use. * * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to setup. * @return \Cake\Http\MiddlewareQueue The updated middleware. */ public function middleware($middleware) { $middleware // Catch any exceptions in the lower layers, // and make an error page/response ->add(\NewRelic\Middleware\NewRelicErrorHandlerMiddleware::class) // Handle plugin/theme assets like CakePHP normally does. ->add(AssetMiddleware::class) // Apply routing ->add(RoutingMiddleware::class); return $middleware; } } ?>