问题
I'm using a Laravel app with an external public directory, e.g. root/Laravel
, and root/html/public
.
I need this app to load from a require on a php file (root/html/this-section.php
) that already has another framework loading it, hence that other fw has its own head, and body tag. This app will load between the header and footer of that file.
When I set up the routes, i notice that both of the following routes work, but i get different results.
Route::get('/', 'HomeController@index');
Route::get('/this-section', 'HomeController@index');
# this also works as a substitute for above, still same issue though
Route::get('//', 'HomeController@index');
These are the same pages, the controller has the same code, I would expect that the pages load the same. Whats happening is the main index site.com/
is loading correctly, but the latter is loading without the html <head>
and <body>
tags which exist on this-section.php
. So essentially, the latter is not loading the parent page, its only loading what it sees on the blade templates.
I suspect maybe either need to add a rewrite to the .htaccess to cover this, or set up a custom redirect or return of a view or response in the controller (I need help here with suggestions) where I can then make a second method, e.g. thisSectionIndex()
and return that suggestion.
At first I had things working with overriding the public_path()
to the correct path this-section
however that backfired when i made the sub routes
e.g. when using the following route setup
Route::get('/', 'HomeController@index');
Route::get('/feature', 'SectionController@feature');
It caused the feature
route to be 500 or 404, I think it couldn't find the files, so I realized I needed to unfortunately add in the /this-section
portion to the route ,which opened a can of worms pretty much.
Route::get('/', 'HomeController@index'); Route::get('/this-section', 'HomeController@index'); Route::get('/this-section/feature', 'SectionController@feature');
ON a side note, some fallout for this which I found a temporary work-around for already, was {{assets()}}
broke. I had assets()
overridden to not have to use the this-section/
portion, e.g. like this {{assets(/this-section/css/styles.css)}}
. The temp worksround was to unfortunately
have to manually add the paths and forego using {{assets()}}
altogether in the blade templates until i figure that part out, because no path is working for them now.
Some background info:
Im overriding the public path like this
#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__.'/../')
);
Here is the pertinent(adjusted) parts of the laravel bootstrapper index (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 is located outside of Laravel, and in 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>
回答1:
With the help of this topic and this topic I found a working solution. Adjustments are needed When your loading in the application. You want to bootstrap it from the same path as the file loads the bootstrap, then override the .htaccess for that particular path only.
move laravels bootstrap file
html/this-section/index.php
file up one directory, next to the filehtml/this-section.php
, and rename it tohtml/this-section-laravel-boostrap.php
adjust the path for laravel on
html/this-section-laravel-boostrap.php
by removing one directory../
- e.g. from
$laravelRelPath = '../../Laravel';
to$laravelRelPath = '../Laravel';
- e.g. from
add the following code to
htlm/this-section.php
(the file loads the bootstrap)
if (strpos($_SERVER['REQUEST_URI'],'this-section') !== false) {
require_once('this-section-laravel-bootstrap.php');
}
- add the following to the .htaccess to snag the uri
#Rewritebase /
RewriteRule ^this-section/?$ / [L,NC]
- The only route needed for
this-section
Route::get('/this-section', 'HomeController@index')
- In your blade templates, call your assets like this so they load on every sub sections url, e.g. for css
<link rel="stylesheet" href="/this-section/css/styles.css">
The application will now bootstrap, and always use the outer contents from the parent file that loads it. See below for an example parent test file(html/this-section.php
) for this.
<!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>
Bonus
assets are now loading correctly, you probably dont need step 5.
{{asset('css/vendors.css')}}
来源:https://stackoverflow.com/questions/46611387/laravel-index-route-issue-when-using-for-a-subsection-in-another-fw-external-pu