Laravel索引路由问题当用于另一个fw(外部公共目录)的子部分时

问题描述:

我正在使用具有外部公共目录的Laravel应用程序,例如, root/Laravelroot/html/publicLaravel索引路由问题当用于另一个fw(外部公共目录)的子部分时

我需要这个应用程序加载从一个PHP文件(root/html/this-section.php)的需求已经有另一个框架加载它,因此,其他FW有它自己的头和身体标记。此应用程序将加载该文件的页眉和页脚之间。

当我设置路线时,我注意到以下两条路线都有效,但我得到了不同的结果。

Route::get('/', '[email protected]'); 
Route::get('/this-section', '[email protected]'); 

# this also works as a substitute for above, still same issue though 
Route::get('//', '[email protected]'); 

这些是相同的页面,控制器具有相同的代码,我希望页面加载相同。发生了什么是主要索引site.com/加载正确,但加载后没有html <head><body>标记存在于this-section.php。所以基本上,后者不会加载父页面,它只能加载它在刀片模板上看到的内容。

我怀疑可能要么需要重写添加到的.htaccess覆盖这一点,或者设置自定义重定向或控制器视图或响应的回报(我需要帮助这里建议)在那里我可以然后制作第二种方法,例如thisSectionIndex()并返回该建议。

起初,我的工作是将public_path()覆盖到正确的路径this-section,但是当我做出子路线 使用以下路线设置

Route::get('/', '[email protected]'); 
Route::get('/feature', 'Section[email protected]'); 

时就引起了feature路线为500或404,我认为它无法找到文件,所以我意识到我需要不幸添加在/this-section部分的路线,这几乎打开了一堆蠕虫。

Route :: get('/','HomeController @ index'); Route :: get('/ this-section','HomeController @ index'); Route :: get('/ this-section/feature','SectionController @ feature');

在附注中,我发现了一个临时解决方案,对此已有一些影响,已经破产了{{assets()}}。我重写了assets()而不必使用this-section/部分,例如,像这样{{assets(/this-section/css/styles.css)}}。临时worksround到unfortunately必须手动添加路径,并放弃在刀片模板中完全使用{{assets()}},直到我找出那部分,因为现在没有路径为他们工作。

一些背景资料:

林覆盖公共路径这样

#Laravel\app\SectionApplication 
class SectionApplication extends \Illuminate\Foundation\Application 
{ 
    public function publicPath() 
    { 
    $newBasePath = $this->basePath . DIRECTORY_SEPARATOR . '..' . 
     DIRECTORY_SEPARATOR . 'html/this-section'; 

    return $newBasePath; 
    } 
} 


#Laravel/bootstrap/app.php 
// $app = new Illuminate\Foundation\Application(
//  realpath(__DIR__.'/../') 
//); 

$app = new App\SectionApplication(
    realpath(__DIR__.'/../') 
); 

这里是laravel引导程序指数(html/this-section/index.php

function public_path($path = '') 
{ 
    return realpath(__DIR__); 
} 

//adjust your relative laravel framework path here 
$laravelRelPath = '../../Laravel'; 

//override the blade {{asset()}} helper 
function asset($path, $secure = null) 
{ 
    $newPubPath = 'this-section/'; //relative from html where the index.php will be 
    return app('url')->asset($newPubPath . $path, $secure); 
} 

的相关(调整后)的部件。htaccess的位于Laravel之外,并在html/

<IfModule mod_rewrite.c> 
    <IfModule mod_negotiation.c> 
     Options -MultiViews 
    </IfModule> 

    RewriteEngine On 
    #Rewritebase /this-section/ 
    #RewriteRule ^(.*)$ this-section/$1 [L] 

    # Redirect Trailing Slashes If Not A Folder... 
    RewriteCond %{REQUEST_FILENAME} !-d 

    # Handle Front Controller... 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule^index.php [L] 

    # Handle Authorization Header 
    RewriteCond %{HTTP:Authorization} . 
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 
</IfModule> 
+0

你** **真的不希望被覆盖'public_path'和'asset'像那。 – ceejayoz

+0

根据推荐的laracast最好的建议。我认为它可能在这里https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5,是问题的根源吗?你会如何推荐?另外,请注意我有资产评论。 – blamb

+0

事实上,虽然这是一个很好的资源链接上面,由于某种原因(支持工匠我相信),我不得不移动到这个解决方案https://*.com/questions/31758901/laravel-5-change-public-path哪是你在我的例子中看到的。 – blamb

随着this topic 的帮助和this topic我找到了一个可行的解决方案。在应用程序中加载时需要进行调整。您希望从文件加载引导时的相同路径引导它,然后仅覆盖该特定路径的.htaccess。

  1. 移动laravels引导文件html/this-section/index.php文件上一级目录,文件旁边html/this-section.php,并通过删除一个目录下并重命名为html/this-section-laravel-boostrap.php

  2. 调整上html/this-section-laravel-boostrap.php为laravel路径../

    • 例如从$laravelRelPath = '../../Laravel';$laravelRelPath = '../Laravel';
  3. 下面的代码添加到htlm/this-section.php(文件加载自举)

if (strpos($_SERVER['REQUEST_URI'],'this-section') !== false) { require_once('this-section-laravel-bootstrap.php'); }

  1. 添加以下至.htaccess拦截uri
  2. #Rewritebase/ RewriteRule ^this-section/?$/[L,NC]

    1. 需要this-section
    2. Route::get('/this-section', '[email protected]')

      的唯一途径
      1. 在刀片模板,打电话给你的资产像这样所以他们加载每个子节的网址,例如对CSS
      2. <link rel="stylesheet" href="/this-section/css/styles.css">

        应用程序现在将引导,并始终使用外部内容从加载它的父文件。请参阅下面的示例父级测试文件(html/this-section.php)。

        <!DOCTYPE HTML> 
        <html> 
        <head> 
            <meta charset="utf-8"> 
            <title>Parent Application</title> 
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
        </head> 
        <body> 
            <header> 
             <nav> 
              <ul> 
               <li>menu</li> 
              </ul> 
             </nav> 
            </header> 
            <?php 
             <--! laravel sandwich --> 
             if (strpos($_SERVER['REQUEST_URI'],'this-section') !== false) { 
              require_once('this-section-laravel-bootstrap.php'); 
             } 
            ?> 
            <footer> 
             <p>Copyright <?php echo date('Y'); ?> - All rights reserved.</p> 
            </footer> --> 
        </body> 
        </html> 
        

        奖金现在 资产被正确加载,你可能不需要步骤5 {{asset('css/vendors.css')}}

      开始=>
    开始=>
开始=>