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:
- 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.
- Next add
'Profiler\ProfilerServiceProvider',
to the list of service providers in app/config/app.php - Next add
'Profiler' => 'Profiler\Facades\Profiler',
to the list of class aliases in app/config/app.php - 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:
- remove
'Profiler\ProfilerServiceProvider',
from the list of service providers in app/config/app.php - remove
'Profiler' => 'Profiler\Facades\Profiler',
from the list of class aliases in app/config/app.php - 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;
}