如何正确使用Laravel 4轮廓仪 [英] How to use Laravel 4 profiler correctly

查看:136
本文介绍了如何正确使用Laravel 4轮廓仪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提高我网站中某些代码的性能,并发现此分析器: https:/ /github.com/loic-sharma/profiler



我遵循了网站上的指导,并在其中一个站点控制器中包含以下内容:

  public function getTest(){

$ logger = new Profiler\Logger\Logger;
$ profiler = new Profiler\Profiler($ logger);
$ profiler-> startTimer('testLogging');




$ data =文章:: select(array(
'articles.id',
'articles.article_date'
'articles.image_link',
'articles.headline',
'articles.category'
)) - > get()
- > toArray );
var_dump($ data);

$ profiler-> endTimer('testLogging');
Log :: info('Hello World!');
echo $ profiler;

在浏览器中,我得到了预期的结果,可以在底部看到分析器栏。 >

我有一个问题:在这个基本测试中,分析器栏,当点击不保持打开,所以我无法查看日志等我不知道为什么或如何解决



如果我删除最终的回声,它可以正常工作。



我可以不过似乎在工具栏中看到计时器'testLogging'。



我是否误解了这个概念?



如何在代码中指定特定功能并显示结果?



谢谢

解决方案

要在Laravel 4中正确使用profiler(loic-sharma / profiler),它不需要您创建对象的实例,例如

  $ logger = new Profiler\Logger\Logger; 
$ profiler = new Profiler\Profiler($ logger);

Laravel有这些美丽的东西叫Facades( http://laravel.com/docs/facades ),分析器实现它们,以便您可以调用Profiler和日志,如下所示:

  public function getTest(){

Profiler :: startTimer('testLogging');

$ data =文章:: select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) - > get()
- > toArray();
var_dump($ data);

Profiler :: endTimer('testLogging');
Log :: info('Hello World!');
}

这不需要你回传$ profiler,所有的输出将被显示在浏览器中的分析器栏中自动进行。



现在,在Profiler之后,请注意 :: ,这通常意味着您正在使用外观,重要的是你明白外观和$ profiler是完全不同的实体。



如果还没有安装外观,或服务提供者做以下:


  1. 首先,您必须使用作曲家安装包,确保
    在作曲家添加后才能运行作曲家更新.json
    require.-你已经这样做了。

  2. 下一步添加'Profiler\ProfilerServiceProvider',到app / config / app.php中的
    提供程序列表

  3. 下一步添加'Profiler'=> 'Profiler\Facades\Profiler',到app / config / app.php中的
    类别列表

  4. 然后在控制台运行 php artisan config:发布
    loic-sharma / profiler

完成之后,上面的修改后的代码应该能够正常工作。



只是为了澄清你做错了什么,你创建了一个新的实例,具有 new Profiler\Logger\Logger; 如果您已经设置了外观设计,则分析器栏将显示(回显)到浏览器中,当您 echo $分析器; 您现在在浏览器中有两个Profilers导致打开的关闭问题,而当您不 echo $ profiler 时,该栏仍然显示。因为它不是你创建因此无法正确显示您的输出的一个



如果你仍然想使用自己的剖析实例:


  1. 删除'Profiler\ProfilerServiceProvider ,从应用程序服务
    提供商列表/配置/ app.php

  2. 除去探查 => 'Profiler\Facades\Profiler',从在应用
    类的别名列表/配置/ app.php

  3. 然后在控制台运行 PHP人员转储自动加载



  4. 然后这将工作:

     公共函数getTest(){

    $记录器=新Profiler\Logger\Logger;
    $ profiler = new Profiler\Profiler($ logger);
    $ profiler-> startTimer('testLogging');

    $数据=文章::选择(阵列(
    'articles.id',
    'articles.article_date',
    'articles.image_link',
    'articles.headline',
    'articles.category'
    )) - > get()
    - > toArray();
    $ logger-> debug(var_dump($ data));

    $ profiler-> endTimer('testLogging');
    $ logger-> info('Hello World!');
    echo $ profiler;
    }


    I'm trying to improve the performance of some code in my site and found this profiler: https://github.com/loic-sharma/profiler

    I've followed the guidance on the site and included the following in one of the sites controllers:

     public function getTest() {
    
        $logger = new Profiler\Logger\Logger;
        $profiler = new Profiler\Profiler($logger);
        $profiler->startTimer('testLogging');
    
    
    
    
        $data = Article::select(array(
            'articles.id',
            'articles.article_date',
            'articles.image_link',
            'articles.headline',
            'articles.category'
        ))  ->get()
            ->toArray();
        var_dump($data);
    
        $profiler->endTimer('testLogging');
        Log::info('Hello World!');
        echo $profiler;
    

    In the browser I get the expected results and can see the profiler bar at the bottom.

    I have one problem: in this basic test the profiler bar, when clicked doesn't stay open so I'm unable to view the logs etc. I'm not sure why or how to go about fixing. THe pane opens then closes again immediately.

    If I remove the final echo it works correctly.

    I can't though seem to see the timer 'testLogging' in the toolbar.

    Have I misunderstood a concept here?

    How can I time specific functions in my code and display the results?

    Thanks

    解决方案

    To use the profiler(loic-sharma/profiler) correctly in Laravel 4 it does not require you the create an instance of the objects for example.

    $logger = new Profiler\Logger\Logger;
    $profiler = new Profiler\Profiler($logger);
    

    Laravel has these beautiful things called Facades (http://laravel.com/docs/facades), the profiler implements them so you can call the Profiler and Log like this:

    public function getTest() {
    
        Profiler::startTimer('testLogging');
    
        $data = Article::select(array(
            'articles.id',
            'articles.article_date',
            'articles.image_link',
            'articles.headline',
            'articles.category'
        ))  ->get()
            ->toArray();
        var_dump($data);
    
        Profiler::endTimer('testLogging');
        Log::info('Hello World!');
    }
    

    This does not require you to echo the $profiler, all output will be displayed in the profiler bar in the browser automatically.

    Notice the :: now after Profiler this usually means you are using the facade, it is important you understand that the facade and the $profiler are completely different entities.

    If you have not yet installed the facades and or service provider do the following:

    1. First you have to install the package with composer, make sure you have run composer update after adding it in your composer.json in "require".- you have already done this.
    2. Next add 'Profiler\ProfilerServiceProvider', to the list of service providers in app/config/app.php
    3. Next add 'Profiler' => 'Profiler\Facades\Profiler', to the list of class aliases in app/config/app.php
    4. Then in the console run php artisan config:publish loic-sharma/profiler

    After you have complete that the amended code above should work perfectly.

    Just to clarify what you did wrong, you created a new Instance of the profiler with new Profiler\Logger\Logger; if you already had the facades set up the profiler bar would be displayed (echoed) to the browser already so when you echo $profiler; you now have two profilers in your browser causing the open close issue, and when you don't echo $profiler the bar is still displayed because it not the one you created thus not showing your output correctly.

    If you still want to use your own instance of the profiler:

    1. remove 'Profiler\ProfilerServiceProvider', from the list of service providers in app/config/app.php
    2. remove 'Profiler' => 'Profiler\Facades\Profiler', from the list of class aliases in app/config/app.php
    3. Then in the console run php artisan dump-autoload

    Then this will work:

    public function getTest() {
    
        $logger = new Profiler\Logger\Logger;
        $profiler = new Profiler\Profiler($logger);
        $profiler->startTimer('testLogging');
    
        $data = Article::select(array(
            'articles.id',
            'articles.article_date',
            'articles.image_link',
            'articles.headline',
            'articles.category'
        ))  ->get()
            ->toArray();
        $logger->debug(var_dump($data));
    
        $profiler->endTimer('testLogging');
        $logger->info('Hello World!');
        echo $profiler;
    }
    

    这篇关于如何正确使用Laravel 4轮廓仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆