2

I've just started working on a Content Management Framework using Laravel 3. It is not designed to be a database-driven app (that may come later). It is designed for web-designers wanting an easy and SEO-friendly way to make a static website.

In a nutshell, the designer would only need to create a default site, and any other [optional] sites (making it a multi-site framework, like Drupal) by means of working with a folder structure.

Currently, the structure has been designed as follows (not implemented yet, it is just an idea; it is also an alteration of Laravel's standard path structure):

public[_html]
root
    app // application folder
    bundles
    sites
        sites.json // used to determine the default site, and register any others
        {site-slug}
            info.json
            content // for page content that inherits a layout
                {page-slug}
                    info.json
                    content.blade.php
            layouts // site layouts: headers, footers, etc.
    stores // storage folder
    system // laravel folder

Here's my main issue right now: I have no idea how to extend the View class to look outside the standard views folder without having to use the path: prefix.

What is the best method of doing this?

Perhaps there is another templating engine that makes this task easier for me? Is there a suitable HAML engine for Laravel 3?

Note: I know there are content bundles that make use of Markdown, but that is not what I'm looking for.

Any help here would be hugely appreciated.

Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81
  • I remember seeing this here (http://laravel.com/docs/bundles#the-basics) - this may be the way to go, as essentially the application folder can be seen as it's own bundle (or site), and other bundles could be used to define other sites. That way you could do routing based on bundles? This answer is obviously not what you're after currently, but hopefully it may be an idea worth pursuing for you. – David Feb 12 '13 at 16:31
  • I did indeed consider it, but I think the architecture will not be suitable. I can only see myself being forced to not be able to be DRY. In addition, I don't want the designer to have to write any code whatsoever. Only XHTML, completely automated. But thanks for the suggestion. :) – Mike Rockétt Feb 12 '13 at 16:37
  • In that case, I remember seeing this a while back: http://forums.laravel.io/viewtopic.php?id=1162 it may have some stuff of use to you. – David Feb 12 '13 at 16:54
  • I'll have a look at that, and see how much of it works for me. Thanks. – Mike Rockétt Feb 12 '13 at 17:33
  • Alternatively - you can extend the view class by creating a library (for the sake of it being autoloaded) and then removing the line in application/config/application.php `'View' => 'Laravel\\View',"` – David Feb 12 '13 at 17:37
  • The answer posted below seems quite promising, and it helps me leave the core alone. Nonetheless, I might find that your suggestion might help as well as the answer below. – Mike Rockétt Feb 13 '13 at 04:56
  • *In order to allow developers to load views outside of the normal loading conventions, we'll allow for a raw path to be given in place of the typical view name, giving total freedom on view loading.* from [`Laravel\\View`](http://laravel.com/api/source-class-Laravel.View.html#61) Seems you have to prefix such a path with `'path: '` (mind the space) – dualed Feb 13 '13 at 08:34
  • @dualed: Like I said in my question, I don't want to have to use that method. If the designer wishes to make changes, I need to be as simple as possible. – Mike Rockétt Feb 13 '13 at 10:45
  • Sorry, it seems that I missed that part. Not completely sure what your problem with the path is though. – dualed Feb 13 '13 at 11:07
  • I was using it - and I guess it can be a viable option. However, I think I'll stick with the solution below (once I've tested it). – Mike Rockétt Feb 13 '13 at 11:12

2 Answers2

2

In the views.php Config file you should be able to add all the directories you want to include to an array

EDIT

Sorry this is for laravel 4. I would extend the view class and have it search an array of paths is the View::path method. You could set this array with the config files so you're call for the paths would be Config::get('views')

MY SOLUTION

A while ago I was working on a project and came up with a solution that avoids modifying any core files.

in application/start.php

Event::listen(View::loader, function($bundle, $view)
{
    foreach (Config::get('views', array()) as $path) {
        if($file = View::file($bundle, $view, $path))
            return $file;
    }

    return View::file($bundle, $view, Bundle::path($bundle).'views');
});

in application/config/views.php (you'll have to create this)

return array(
    path('app').'../myviews',
);

Now you can add any number of directories to this file and they will be checked before the default view directory is checked.

Blessing
  • 4,858
  • 1
  • 18
  • 13
  • I'm battling to understand what you're trying to say here. Does this involve editing the Laravel core? Perhaps I'm not entirely sure what the `View::path` method does... – Mike Rockétt Feb 12 '13 at 17:36
  • I've been working on laravel 4 so I'm a bit rusty. However, I did find an old project I implemented this feature in and I did not have to modify the core or create any classes. I will update my answer with the code. – Blessing Feb 12 '13 at 18:56
  • That looks very promising. I knew it had something to do with the loader! I'll give that a try this afternoon, and let you know how it goes. – Mike Rockétt Feb 13 '13 at 04:50
-1

I am using this:

Create Middleware setDB:

namespace App\Http\Middleware; 

use Cookie;  
use Config;  
use Closure;  
use DB;  
use App\User;  
use App\Common;  
use Auth;  
use Session;  
use View;  
use Illuminate\Contracts\Foundation\Application;   

class SetDB
{  
   public function __construct(Application $app){   
       $this->app=$app;  
   }  
   public function handle($request, Closure $next)  
   {  
        $server=$_SERVER["SERVER_NAME"];  
        $paths = 'resources/views';    
        $asset = 'public';  
        $result=DB::connection('mysql2')->table('mst_editor_database_info')->where("paths",$server)->first();   
        DB::disconnect(env('DB_DATABASE'));

        if(count($result) > 0){ 

            $dbname=$result->dbname;   
            $paths = 'themes/'.$result->paths.'/views';   
            Session::put('paths',$paths);  
            Session::put('asset','themes/'.$result->paths.'/assets');   
            Config::set('database.connections.mysql', array(
                   'driver' => 'mysql',
                   'host' => env('DB_HOST'),
                   'port' => env('DB_PORT'),
                   'database' =>$dbname,
                   'username' => 'username',
                'password' => 'password',
                'charset' => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix' => '',
            ));
            DB::reconnect('mysql');

        }else{

            DB::disconnect('mysql');
            Config::set('database.connections.mysql', config('database.connections.mysql2'));
            DB::reconnect('mysql');
            Session::put('paths',$paths);
            Session::put('asset',$asset);
        }
        Common::setTheme();
        Config::set('site.asset', Session::get('asset'));
        return $next($request);
    }
}

Create class App\Common.php:

namespace App;
use Config;
use Session;
use Illuminate\Database\Eloquent\Model;
use View;

class Common extends Model
{
   public static function setTheme(){
       Config::set('view.paths', Session::get('paths'));
       View::addNamespace('Theme', Session::get('paths'));
    }
}

Using Namespace Theme:

return View::make('Theme::welcome');

register middleware in kernel.php :

protected $middlewareGroups = [
  'web' => [
    \App\Http\Middleware\SetDB::class,
  ]
];

Set default db connection in your config/database.php:

'connections' => [

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],

],

Set config/view.php:

    'paths' => [base_path('/')],

Create your template folders

themes/{paths}/views
chrki
  • 6,143
  • 6
  • 35
  • 55
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – DimaSan Dec 21 '16 at 08:25