Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel load view from a folder name with actual dot

I am creating a multi-domain Laravel app, so my view files are in separate folders per domain. For example, the following:

return view('pages/' . $_SERVER['SERVER_NAME'] . '/public/home', []);

should load a view under

pages/domain.com/public/home.blade.php

but instead it attempts to load

pages/domain/co/public/home.blade.php

because of the dot notation.

How do I get around this?

like image 519
Obay Avatar asked Nov 15 '15 15:11

Obay


2 Answers

You'd need to add a view namespace to set up hints for a particular folder if there're dots in the name.

$domain = 'domain.com';

View::addNamespace($domain, config('view.paths')[0]."/{$domain}/");

Route::get('example', function() use ($domain) {
    return view("{$domain}::home");
});

You could use base_path('resources/views') instead of in the example above config('view.paths')[0] which is probably a bit more sensible in case someone reorders or changes the value of config('view.paths')

like image 169
Ben Swinburne Avatar answered Oct 10 '22 11:10

Ben Swinburne


Maybe you could replace the dot with underscore:

$domain = str_replace('.', '_', $_SERVER['SERVER_NAME']);
return view('pages/' . $domain . '/public/home');

and load the view under:

pages/domain_com/public/home.blade.php
like image 35
Pantelis Peslis Avatar answered Oct 10 '22 11:10

Pantelis Peslis