catthr
8/25/2016 - 11:43 AM

october middleware bot spider google

october middleware bot spider google

Добавляем файл с middleware

<?php

namespace Develex\AbStatistics\Classes\Http;

use Closure;
use Illuminate\Http\Request;

class BotsMiddleware {
    /**
     * Filter request from spiders and bots by User-Agent
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if(preg_match('/bot|crawl|slurp|spider/i', $request->header('user-agent', ''))){
            return '';
        }
        return $next($request);
    }
}

Регистрация

На группу маршрутов

Route::group(['middleware' => 'Develex\AbStatistics\Classes\Http\BotsMiddleware'], function(){
   Route::get('log', ['uses' => 'Develex\AbStatistics\Controllers\StatisticsController@log']);
});

В plugin.php в boot

    // Add a new middleware to beginning of the stack.
    $this->app['Illuminate\Contracts\Http\Kernel']
         ->prependMiddleware('Path\To\Custom\Middleware');
         
    // Add a new middleware to end of the stack.
    $this->app['Illuminate\Contracts\Http\Kernel']
         ->pushMiddleware('Path\To\Custom\Middleware');

Или так

Cms\Classes\CmsController::extend(function($controller) {
   $controller->middleware('your middleware here');
});