将Laravel包含在自定义PHP脚本中并路由到控制器


Include Laravel into custom PHP script and routing to a controller

假设有一个基于 Web 的 PHP 项目,它使用一个独立的框架,并且还包含一个包含 Laravel 4.2 的文件夹:

project/
    laravel/ (Laravel install)
        app/
            routes.php
        public/
            index.php
    index.php
    test.php
    .htaccess

.htaccess文件将每个查询重写到独立框架的index.php,除非请求PHP脚本(例如,project/test.php)。

在这种情况下,test.php 包含包含 Laravel 索引.php脚本的require laravel/public/index.php。用于访问此内容的 URI http://example.com/test.php

我怎样才能利用拉拉维尔的路由来获取这个脚本?我试过这个:

Route::get('/test.php', function()
{
    echo 'success';exit;
});

但这不起作用(我尝试过测试.php只是测试)。转储Route::getCurrentRoute()输出以下内容:

object(Illuminate'Routing'Route)[126]
  protected 'uri' => string '/' (length=1)
  ...
  protected 'compiled' => 
    object(Symfony'Component'Routing'CompiledRoute)[135]
      ...
      private 'staticPrefix' => string '/' (length=1)
      ...

是否可以访问http://example.com/test.php并让 Laravel 的路由看到它,就好像已经请求了/test/test.php,而无需更改独立框架的 .htaccess 文件?

你不会被迫使用Laravel路由,你可以用纯PHP绕过它:

if($_SERVER['REQUEST_URI'] === '/test.php')
{
    exit('success');
}

但是在您的情况下,当Laravel不处理页面时.php您可能应该使用.htaccess将所有请求重定向到Laravel并回退到根索引:

App::missing(function($exception)
{
    include('../../index.php');
    exit;
});