src/AppBundle/Controller/AutoRouteController.php line 9

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. class AutoRouteController extends Controller
  6. {
  7.     public function anyAction(Request $request)
  8.     {
  9.         $path rtrim($request->getPathInfo(), '/');
  10.         // ディレクトリトラバーサル脆弱性に注意
  11.         $path str_replace('..'''$path);
  12.         $rootDir $this->get('kernel')->getRootDir();
  13.         $folder realpath($rootDir '/../../../');
  14.         // 除外するファイルのパターン(.htaccess でも制御可)
  15.         $excludes = array(
  16.             '/^\/admin\//',
  17.             '/^\/modules\//',
  18.         );
  19.         foreach($excludes as $pattern){
  20.             if(preg_match($pattern$path)){
  21.                 throw $this->createNotFoundException('Page not found.');
  22.             }
  23.         }
  24.         if(file_exists($folder $path '/index.php')){
  25.             $template $folder $path '/index.php';
  26.         } else if(file_exists($folder $path '.php')){
  27.             $template $folder $path '.php';
  28.         } else {
  29.             throw $this->createNotFoundException('Page not found.');
  30.         }
  31.         return $this->render(realpath($template), [
  32.         ]);
  33.     }
  34. }