File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/KommaApp/Shop/Routes/RouteGenerator.php
<?php
namespace KommaApp\Shop\Routes;
use KommaApp\Shop\Shop;
class RouteGenerator
{
protected $table = 'routes';
protected $shop;
function __construct(Shop $shop)
{
$this->shop = $shop;
}
/*
* Generate the new route
*/
public function generate()
{
// Get all the routes from the current shop
$routes = Route::whereShopId($this->shop->getId())->orderBy('route','desc')->get();
// loop through the routes
foreach($routes as $key => $route)
{
// Check the routeString
$routeString = '/' . $route->route . '/{any?}';
// Replace '//' in case an empty string (f.e. home = //{any?} )
$routeString = str_replace('//','/',$routeString);
// Search for the route
\Route::get($routeString, function($any = null) use ($route){
// Get the controller name through a routable query
$controller = $route->routable->getController();
// Set a default method
$method = 'index';
// Check if a method is included in the controller string
if($controllerData = $this->parseController($controller))
{
$controller = $controllerData['controller'];
$method = $controllerData['method'];
}
// Return a new controller that allows dependency injection
return \App::make($controller)->{$method}($any);
// Make sure all the remaining parameters are injected into the method
})->where('any', '(.*)?');
}
}
/*
* Does the controller have a method specified
*/
private function parseController($controller)
{
$temp = explode('@',$controller);
if(count($temp) > 1)
{
return [
'controller' => $temp[0],
'method' => $temp[count($temp)-1]
];
}
return false;
}
}