File: D:/HostingSpaces/SBogers10/csb.komma.pro/app/Base/Controller.php
<?php
namespace App\Base;
use App\Pages\PageService;
use App\Routes\Models\Route;
use Komma\KMS\Globalization\Languages\LanguageService;
use Komma\KMS\Sites\Models\Site;
use Komma\KMS\Sites\SiteServiceInterface;;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $pageService;
protected $links;
/** @var SiteServiceInterface */
protected $siteService;
/** @var Site */
protected $site;
public function __construct()
{
$this->preventRestRouteFromWorking();
$this->pageService = new PageService();
$this->links = $this->pageService->getAllTranslatedPageRoutes();
}
/**
* This will keep track of the pagination page of the given pagination key
* We use this to be able to create the correct return to overview route
*/
protected function keepTrackOfPagination(string $modelPaginationKey, $paginationKey = 'page')
{
// Create object for pagination
$pagination = (object)[
'key' => $paginationKey,
'page' => request()->get($paginationKey, 1)
];
// Store and save session
session([$modelPaginationKey => $pagination]);
session()->save();
}
/**
* Create route by merging the pagination key and the route
*
* @param string $modelPaginationKey
* @param string $route
* @return string
*/
protected function createPreviousRoute(string $modelPaginationKey, string $route)
{
// If session is null or on the first page, we need the normal route
if(!$pagination = session($modelPaginationKey, null)) return $route;
if($pagination->page == 1) return $route;
return $route . '?' .$pagination->key . '=' . $pagination->page;
}
/**
* This method will prevent that REST routes are working with the application
* They should be resolved by the middleware
*
*/
private function preventRestRouteFromWorking(){
if (app()->runningInConsole()) return;
$request = request();
// Normally it should be resolved or it's a hard defined route
// Example. contact/process
if(isset($request->resolved) && $request->resolved) return;
else{
// Get segments and path
$path = request()->path();
$segments = request()->segments();
// Check if current path is a restfull path, or the first segment of it is
$restRoutes = Route::whereIn('route', [$path, $segments[0]])
->get();
// Rest routes should be empty, or it already should be resolved
if($restRoutes->count() == 0) return;
else{
// If production throw 404
if(app()->environment() == 'production') throw abort(404);
// Else Argument Exception with explanation
else throw new \InvalidArgumentException("Route path or the first segment is listed as an REST route (Path: '".$path."' First segment: '".$segments[0]."')");
}
}
}
}