Laravel:跟踪页面浏览量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21380967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Tracking page views
提问by Paulo Freitas
I'm trying to implement a very simple page tracking system with Laravel, just to know which pages are most accessed.
我正在尝试用 Laravel 实现一个非常简单的页面跟踪系统,只是为了知道哪些页面被访问最多。
At first I thought of create a table with access date, request URL (from Request::path()
) and user id, as simple as that.
起初我想创建一个包含访问日期、请求 URL(来自Request::path()
)和用户 ID 的表,就这么简单。
But I'd have to show page titles on reports, and for that I need some way to translate the request URI to its page title. Any ideas for that? Is there a better way to accomplish this?
但是我必须在报告上显示页面标题,为此我需要某种方式将请求 URI 转换为其页面标题。有什么想法吗?有没有更好的方法来实现这一点?
Currently I set page titles from Blade view files, through @section('title', ...)
.
目前我从 Blade 视图文件设置页面标题,通过@section('title', ...)
.
Thank you in advance!
先感谢您!
回答by Alexandre Butynski
You can use Google Analytics to show pretty and very efficient reports. With the API, you can also customize the reports and (for example) show the pages titles.
您可以使用 Google Analytics 来显示漂亮且高效的报告。使用 API,您还可以自定义报告和(例如)显示页面标题。
If you want to develop it by yourself, I think that the best solution is to write an after filter that can be called after each page loading. One way of setting the title, in this case, is to use flash session variable (http://laravel.com/docs/session#flash-data) :
如果要自己开发,我觉得最好的办法就是写一个after filter,每次页面加载后都可以调用。在这种情况下,设置标题的一种方法是使用 flash 会话变量 ( http://laravel.com/docs/session#flash-data):
// routes.php
Route::group(array('after' => 'log'), function()
{
Route::get('users', 'UserController@index');
}
// filters.php
Route::filter('log', function() {
Log::create(array('title' => Session::get('title'), '...' => '...'));
}
// UserController.php
public function index()
{
Session::flash('title', 'Users Page');
// ...
}
// layout.blade.php
<head>
<title>{{ Session::get('title') }}</title>
...
回答by marcanuy
I know this has already been answered but I would like to add that I've published a package that can be easily implemented to track page views for Laravel (if they are Eloquent ORM instances) in specific date ranges: last day, week, month or all time.
我知道这已经得到了回答,但我想补充一点,我已经发布了一个可以轻松实现的包,可以在特定日期范围内跟踪 Laravel(如果它们是 Eloquent ORM 实例)的页面浏览量:最后一天、一周、一个月或所有时间。