Question

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.

Était-ce utile?

La solution

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.

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top