File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/app/Http/Controllers/Controller.php
<?php
namespace App\Http\Controllers;
use App\KommaApp\Images\ImageService;
use App\KommaApp\Languages\LanguageService;
use App\KommaApp\Pages\PageService;
use App\KommaApp\Routes\Models\Route;
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 $languageService;
protected $pageService;
protected $links;
protected $baseViewPath = 'site.';
public function __construct()
{
$this->preventRestRouteFromWorking();
$this->languageService = new LanguageService();
$this->pageService = new PageService();
$this->links = $this->pageService->getAllTranslatedPageRoutes();
}
/*
* This function should only be temporary until dynamic blocks is rewrite to it's own blocks
* instead of using json
*/
protected function decodeDynamicContent($translation)
{
$decodedJson = json_decode($translation->description);
$dynamicBlocks = [];
$dynamicGroups = [];
$imageController = new ImageService();
foreach ($decodedJson as $dynamicElement){
if(isset($dynamicElement->fileIds)) {
$images = $imageController->getDynamicBlockImages( $dynamicElement->fileIds );
$dynamicElement->images = $images;
}
// Order dynamic blocks
if($dynamicElement->code_name != '' ){ // If dynamic block has code name then add it to the translation
$translation->{$dynamicElement->code_name} = $dynamicElement;
} elseif($dynamicElement->view != '' && $dynamicElement->typeSlug != 'view-block'){ // If dynamic block has view then add it to dynamic groups
if( !isset($dynamicGroups[$dynamicElement->view])) $dynamicGroups[$dynamicElement->view] = [];
$dynamicGroups[$dynamicElement->view][] = $dynamicElement;
} else{ // Else just add them to the default dynamic content
$dynamicBlocks[] = $dynamicElement;
}
}
$translation->groups = $dynamicGroups;
$translation->description = $dynamicBlocks;
return $translation;
}
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($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 is listed as an REST route");
}
}
}
}