Laravel 5.2 - Using route parameters in middleware
Route::group(['middleware' => ['web']], function () {
Route::get('/{purl?}', function ($purl = 'None') {
// handle route
});
});
<?php namespace App\ViewComposers;
use Config;
use Log;
use PurlGurl;
use Session;
use Illuminate\Contracts\View\View;
class PurlGurlViewComposer
{
/**
* Create a new composer.
*
* @return void
*/
public function __construct()
{
}
/**
* Bind data to the view.
*
* @param View $view
*
* @return void
*/
public function compose(View $view)
{
Log::debug(__METHOD__ . ' : bof' );
// The middleware retrieves the responder and places their details into the session
$responder = Session::has('responder') ? Session::get('responder') : [];
$view->with(compact('responder'));
}
}
<?php namespace App\Http\Middleware;
/**
* The Purl Middleware is applied to routes that we want to protect by making sure that people who access
* them are recognized.
*/
use Closure;
use Config;
use Input;
use Log;
use PurlGurl;
use Request;
use Session;
class PurlGurlMiddleware
{
/**
* Create a new filter instance.
*
* @param Guard $auth
* @return void
*/
public function __construct()
{
}
/**
* Handle an incoming request.
*
* We first check if the responder details are available in the session.
* If they're not we attempt to get them from the PURL
* If we're not able to get them from the PURL then we redirect the person to the root if the config says so
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $required Whether we must redirect if there are no responder details
* @return mixed
*/
public function handle(\Illuminate\Http\Request $request, Closure $next, $required = true)
{
PurlGurl::trackVisit($request);
return $next($request);
}
}
<?php namespace App\Lib\Purlgurl;
/**
* Class Purlgurl
*
* This class provides the purlgurl functionality - tracking visitors, and obtaining the responder from the request
*
*/
use App;
use Config;
use DB;
use Exception;
use Log;
use \Illuminate\Http\Request as Request;
use Session;
class PurlGurl
{
/**
* @var string
*/
public $client;
/**
* @var string
*/
public $project_type;
/**
*
*/
public function __construct()
{
}
/**
* trackVisit
* @access public
*
* Tracks the visit to a page and associates the visitor with a responder
*
* @param $request
*/
public function trackVisit(Request $request)
{
Log::debug(__METHOD__ . ' : bof');
$responder = $this->retrieveResponder($request);
// we have the responder so log the visit in the database
}
/**
* getResponder
* @access public
*
* Retrieves the current responder details from the request. This is used in the Middleware.
*
*/
public function retrieveResponder(Request $request)
{
Log::debug(__METHOD__ . ' : bof');
// does this person have a responder in session?
if (Session::has('responder')) {
return Session::get('responder');
} else {
// has this person sent a purl in the url? if so retrieve the input, create a responder, set the session
$parameters = $request->route()->parameters();
if (isset($parameters['purl']) {
} else {
// does this person have a responder stored in their cookie?
// if no responder create a fresh blank responder and set the cookie tying the visitor to the responder
}
}
}
}