File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Base/Controller.php
<?php
namespace App\Komma\Base;
use App\Komma\Globalization\Languages\LanguageService;
use App\Komma\Locations\LocationService;
use App\Komma\Pages\PageService;
use App\Komma\Routes\Models\Route;
use App\Komma\Sites\Models\Site;
use App\Komma\Sites\SiteServiceInterface;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $languageService;
protected $pageService;
protected $locationService;
protected $links;
protected $baseViewPath = 'site.';
/** @var SiteServiceInterface */
protected $siteService;
/** @var Site */
protected $site;
public function __construct()
{
if (app()->runningInConsole()) {
return;
}
$this->preventRestRouteFromWorking();
$this->languageService = new LanguageService();
$this->pageService = new PageService();
$this->locationService = new LocationService();
$this->siteService = app(SiteServiceInterface::class);
$this->site = $this->siteService->getCurrentSite();
$this->links = $this->pageService->getAllTranslatedPageRoutes();
if (isset($this->links->locations)) {
$this->links->locations->items = $this->locationService->getLocations();
}
}
/**
* 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') {
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]."')");
}
}
}
}
// TODO: Add this to the linker / Links class
protected function keepTrackOfPagination(string $modelPaginationKey, $paginationKey = 'page')
{
// Create object for pagination
$pagination = (object) [
'key' => $paginationKey,
'page' => \Input::get($paginationKey, 1),
];
// Store and save session
session([$modelPaginationKey => $pagination]);
session()->save();
}
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;
}
}