Commit 0e86d150 authored by nirgendswo's avatar nirgendswo

delete slug middleware

adding middleware
parent 0e961873
......@@ -17,6 +17,9 @@ class PageController extends Controller
*/
public function showAction($slug = NULL)
{
$pageService = app(\App\PageService::class);
$slug = $pageService->getSlug();
// get current page with slug
$page = PageRepository::findOne($slug);
......
......@@ -4,10 +4,8 @@ namespace App\Http\Middleware;
use Closure;
class SlugMiddleware
class parameters
{
const REGEX_P = '/\/p\/[0-9]*$/';
/**
* Handle an incoming request.
*
......@@ -15,16 +13,9 @@ class SlugMiddleware
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
public function handle($request, Closure $next)
{
$p = NULL;
preg_match(self::REGEX_P, $slug, $p);
if ($p) {
$slug = preg_replace(self::REGEX_P, '', $slug);
}
$pageService = app(\App\PageService::class);
return $next($request);
}
}
<?php
namespace App\Services;
use Illuminate\Http\Request;
/**
* PageService
*
*
* @author Björn Hase
*/
class PageService {
/** getting /p/? from slug */
const REGEX_PAGE = '/\/p\/[0-9]*$/';
const REGEX_PAGE_STRING = '/\/p\//';
/** getting /tags/? from slug */
const REGEX_TAGS = '/\/tags\/[a-zA-Z0-9,]*$/';
const REGEX_TAGS_STRING = '/\/tags\//';
/** */
private $page = NULL;
/** */
private $tags = NULL;
/** */
private $slug = NULL;
/**
*
*
*/
public function __construct(Request $request)
{
$this->slug = '/'.$request->path();
$this->handlePage($this->slug);
$this->handleTags($this->slug);
}
/**
*
*
* @param String $slug
*/
private function handlePage($slug)
{
preg_match(self::REGEX_PAGE, $slug, $this->page);
if ($this->page) {
$this->page = preg_replace(self::REGEX_PAGE_STRING, '', $this->page);
$this->page = $this->page[0];
$this->page = intval($this->page);
$this->slug = preg_replace(self::REGEX_PAGE, '', $this->slug);
}
}
/**
*
*
* @param String $slug
*/
private function handleTags($slug)
{
preg_match(self::REGEX_TAGS, $slug, $this->tags);
if ($this->tags) {
$this->tags = preg_replace(self::REGEX_TAGS_STRING, '', $this->tags);
$this->tags = $this->tags[0];
$this->tags = explode(',', $this->tags);
$this->slug = preg_replace(self::REGEX_TAGS, '', $this->slug);
}
}
/**
*
*
* @return integer
*/
public function getPage()
{
return $this->page;
}
/**
*
*
* @return array
*/
public function getTags()
{
return $this->tags;
}
/**
*
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
}
......@@ -46,6 +46,25 @@ $app->singleton(
App\Console\Kernel::class
);
$app->singleton('App\PageService', function($app) {
return new App\Services\PageService($app->make('request'));
});
/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/
$app->routeMiddleware([
'parameters' => App\Http\Middleware\Parameters::class,
]);
/*
|--------------------------------------------------------------------------
| Register Service Providers
......
......@@ -16,6 +16,13 @@ $app->get('/', [
]);
// get the hole url as slug
$app->get('/{slug:[0-9\/a-z]+}', [
'as' => 'page.show', 'uses' => 'PageController@showAction'
]);
$app->group(['middleware' => 'parameters'], function() use ($app) {
$app->get('/{slug:[0-9\/a-z,]+}', [
'as' => 'page.show', 'uses' => 'PageController@showAction'
]);
});
// any other router will be result in a 404
$app->get('{any}', function() {
return abort(404);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment