我刚刚开始使用Laravel 3开发内容管理框架。它不是一个数据库驱动的应用程序(可能会在以后出现)。它专为网页设计师而设计,他们希望以简单易懂的SEO方式制作静态网站。
简而言之,设计人员只需要通过使用文件夹结构来创建默认站点和任何其他[可选]站点(使其成为多站点框架,如Drupal)。
目前,该结构的设计如下(尚未实现,它只是一个想法;它也是Laravel标准路径结构的改变):
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
这是我现在的主要问题:我不知道如何将View类扩展到标准views
文件夹之外,而不必使用path:
前缀。
这样做的最佳方法是什么?
也许有另一个模板引擎让我更容易完成这项任务? Laravel 3有适合的HAML引擎吗?
注意:我知道有些内容包使用了Markdown,但这不是我想要的。
这里的任何帮助都将非常感激。
答案 0 :(得分:2)
在views.php配置文件中,您应该能够将要包含的所有目录添加到数组中
修改
很抱歉这是laravel 4.我会扩展视图类并让它搜索一个路径数组是View :: path方法。您可以使用配置文件设置此数组,因此您调用路径将是Config :: get('views')
我的解决方案
前段时间我正在开发一个项目,并提出了一个避免修改任何核心文件的解决方案。
在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');
});
在application / config / views.php中(你必须创建它)
return array(
path('app').'../myviews',
);
现在,您可以向此文件添加任意数量的目录,并在检查默认视图目录之前检查它们。
答案 1 :(得分:-1)
我正在使用它:
创建中间件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);
}
}
创建类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'));
}
}
使用命名空间主题:
return View::make('Theme::welcome');
在kernel.php中注册中间件:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\SetDB::class,
]
];
在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,
],
],
设置config / view.php:
'paths' => [base_path('/')],
创建模板文件夹
themes/{paths}/views