0

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>
blamb
  • 4,220
  • 4
  • 32
  • 50
  • You **really** don't want to be overriding `public_path` and `asset` like that. – ceejayoz Oct 06 '17 at 17:59
  • According to the best suggestions on laracast that was recommended. I think it may have been here https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5, is that the source of the problem? how would you recommend? Also, notice i have asset commented. – blamb Oct 06 '17 at 18:01
  • Actually while thats a good resource linked above, for some reason (to support artisan I believe), I had to move to this solution https://stackoverflow.com/questions/31758901/laravel-5-change-public-path which is what you see in my example. – blamb Oct 06 '17 at 18:08

1 Answers1

0

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.

  1. move laravels bootstrap file html/this-section/index.php file up one directory, next to the file html/this-section.php, and rename it to html/this-section-laravel-boostrap.php

  2. adjust the path for laravel on html/this-section-laravel-boostrap.php by removing one directory ../

    • e.g. from $laravelRelPath = '../../Laravel'; to $laravelRelPath = '../Laravel';
  3. 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'); }

  1. add the following to the .htaccess to snag the uri

#Rewritebase / RewriteRule ^this-section/?$ / [L,NC]

  1. The only route needed for this-section

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

  1. 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')}}

blamb
  • 4,220
  • 4
  • 32
  • 50