php - 将 Laravel 包含到自定义 PHP 脚本中并路由到 Controller

标签 php url laravel laravel-4 routing

假设有一个基于 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 包含 require laravel/public/index.php 以包含 Laravel 的 index.php 脚本。访问它的 URI 是 http://example.com/test.php

如何使用 Laravel's routing拿起这个剧本?我试过这个:

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

但这不起作用(我试过 test.phptest)。转储 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');
}

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

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

关于php - 将 Laravel 包含到自定义 PHP 脚本中并路由到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27839508/

相关文章:

php - 自定义帖子类型元框单选按钮输出

javascript - 删除 URL 中的最后一个目录

php - 如何在 Laravel 中限制资源丰富的路由

laravel - 在查询中过滤(搜索引擎)的最佳方法

java - tomcat 端口 URL 限制

mysql - 如何在 laravel.. 中实现 Select 查询?

php - html页面中的数据库mysql

php - 如何在 Symfony2 中使用一堆不相关的实体制作表单?

php - 图片上传插件(php jquery flash)

php - 使用 mod_rewrite RewriteRule 重写所有查询以不需要 .php 扩展名