<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class AutoRouteController extends Controller
{
public function anyAction(Request $request)
{
$path = rtrim($request->getPathInfo(), '/');
// ディレクトリトラバーサル脆弱性に注意
$path = str_replace('..', '', $path);
$rootDir = $this->get('kernel')->getRootDir();
$folder = realpath($rootDir . '/../../../');
// 除外するファイルのパターン(.htaccess でも制御可)
$excludes = array(
'/^\/admin\//',
'/^\/modules\//',
);
foreach($excludes as $pattern){
if(preg_match($pattern, $path)){
throw $this->createNotFoundException('Page not found.');
}
}
if(file_exists($folder . $path . '/index.php')){
$template = $folder . $path . '/index.php';
} else if(file_exists($folder . $path . '.php')){
$template = $folder . $path . '.php';
} else {
throw $this->createNotFoundException('Page not found.');
}
return $this->render(realpath($template), [
]);
}
}