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>