themonkeys / laravel-silverstripe
将 Silverstripe CMS 集成到您的 Laravel 应用程序中
Requires
- php: >=5.3.0
- illuminate/support: ~4.0
This package is not auto-updated.
Last update: 2024-09-23 15:10:22 UTC
README
Laravel 的 Silverstripe 适配器
我们想使用出色的 Silverstripe CMS,同时保留 Laravel 提供的出色应用程序开发框架,所以我们找到了一种方法来同时使用两者。
本软件包提供
- 一层薄薄的层,可以从 Laravel 代码中访问 Silverstripe 模型对象
- 一种新类型的路由,允许 Laravel 路由处理在 Silverstripe CMS 中定义的页面 URL
- 根据您的 Laravel 配置自动配置 Silverstripe 的数据库设置
- 自动将 Silverstripe 的日志系统与 Laravel 的日志系统耦合
要求
- Laravel 4.1.x:如果您需要使用 Laravel 4.0.x,您将需要使用本软件包的 v1.0 版本。当前版本仅适用于 Laravel 4.1.x。
安装
安装软件包
要获取软件包的最新版本,只需在 composer.json 文件中添加它,并运行
composer require themonkeys/laravel-silverstripe:dev-master --no-update composer update themonkeys/laravel-silverstripe
安装后,您需要将服务提供程序注册到应用程序中。打开 app/config/app.php 并找到 providers 键。
'providers' => array( 'Themonkeys\Silverstripe\SilverstripeRoutingServiceProvider', )
该软件包通过外观提供了一些常用 CMS 功能。为了便于使用,您还可以将其添加到 app/config/app.php 文件中的别名中
'aliases' => array( 'CMS' => 'Themonkeys\Silverstripe\Silverstripe', )
遗憾的是,Silverstripe 中的一些类名与 Laravel 默认 app.php 配置中定义的别名冲突(幸运的是,Laravel 使用命名空间,所以我们并没有完全陷入困境)。我们迄今为止找到的最佳解决方案需要您做一些工作……在 app/config/app.php 文件中将以下别名重命名,并在必要的地方继续使用您的工作中的新别名
'aliases' => array( 'L_Config' => 'Illuminate\Support\Facades\Config', 'L_Controller' => 'Illuminate\Routing\Controllers\Controller', 'L_Cookie' => 'Illuminate\Support\Facades\Cookie', 'L_File' => 'Illuminate\Support\Facades\File', 'L_Form' => 'Illuminate\Support\Facades\Form', 'L_Session' => 'Illuminate\Support\Facades\Session', 'L_Validator' => 'Illuminate\Support\Facades\Validator', )
您不必使用 L_,您可以使用任何您喜欢的。
除了 blade 模板之外的所有地方,我们更喜欢使用 use 语句,然后我们可以继续使用正常的 Laravel 名称。例如,假设您有一个需要使用 Laravel 的 Validator 的控制器。按照这种方式编写代码
<?php use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Validator; class YourController extends BaseController { ... public static function postForm() { Input::flash(); $validator = Validator::make(Input::all(), static::$rules); ... } }
重要:随 Laravel 一起提供的默认
app/controllers/BaseController.php依赖于其父类的Controller别名作为名称,因此您需要将Controller重命名为L_Controller或添加一个use语句
<?php use Illuminate\Routing\Controller; class BaseController extends Controller {
安装 Silverstripe
访问 http://www.silverstripe.org/ 并决定您想使用哪个版本的 Silverstripe。我们已经测试了与版本 3.0.5、3.1.0-beta3 和 3.1.2 的 Laravel 集成。
-
在您的 Laravel 项目中创建一个名为
public/silverstripe/的文件夹。 -
在该文件夹中安装 Silverstripe。例如,要安装版本 3.1.2,从您的项目的根目录执行以下操作
composer create-project silverstripe/installer ./public/silverstripe/ 3.1.2
对于 Silverstripe 的 3.0.x 版本,脚本会以错误结束,但请放心,那只是因为 Silverstripe 的数据库连接细节尚未设置。对于我们来说,3.1.2 版本的安装没有错误。
注意:如果命令建议您创建一个
_ss_environment.php文件,请不要这样做。我们将使用 Laravel 的环境支持来配置 Silverstripe。 -
在 Laravel 重写规则之前,将以下内容添加到您的
.htaccess文件中 之前# ------------------------------------------------------------------------------ # | Silverstripe CMS | # ------------------------------------------------------------------------------ <IfModule mod_rewrite.c> RewriteRule ^admin/?$ /silverstripe/admin/ [R,L] RewriteRule ^assets/(.*)$ /silverstripe/assets/$1 [L] </IfModule>
-
在您的
.htaccess文件中的 Laravel 重写规则的第一行添加一个银色条RewriteCond行# ------------------------------------------------------------------------------ # | Laravel framework | # ------------------------------------------------------------------------------ <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On # Redirect Trailing Slashes... RewriteCond %{REQUEST_URI} !^/silverstripe RewriteRule ^(.*)/$ /$1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_URI} !^/silverstripe RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>
-
我们将通过Laravel而不是Silverstripe内置的MVC框架来交付CMS内容页面;因此,禁用默认的
/silverstripe/内容URL是个好主意。您可以通过将以下404规则添加到Silverstripe附带现有403规则之后的public/silverstripe/.htaccess文件中来实现这一点。<IfModule mod_alias.c> RedirectMatch 403 /silverstripe-cache(/|$) RedirectMatch 403 /vendor(/|$) RedirectMatch 403 /composer\.(json|lock) # Only allow the CMS admin and dev-related silverstripe URLs. RedirectMatch 404 /silverstripe/?$ RedirectMatch 404 /silverstripe/(?!admin|assets|cms|framework|Security|themes|dev|gridfieldextensions) </IfModule>
删除Silverstripe的安装文件
Laravel负责处理Silverstripe的设置,因此您可以立即删除安装文件
rm public/silverstripe/install*
配置Silverstripe
与Silverstripe一样,您的自定义代码应放在public/silverstripe/mysite文件夹中。如果您想将mysite重命名为其他名称,现在是时候了。
mv public/silverstripe/mysite public/silverstripe/awesomesauce
以下说明将继续使用名称mysite。
编辑public/silverstripe/mysite/_config.php文件,并替换以下行:
global $database; $database = ''; require_once('conf/ConfigureFromEnv.php'); MySQLDatabase::set_connection_charset('utf8');
为:
require_once __DIR__.'/../../../bootstrap/autoload.php'; Themonkeys\Silverstripe\Laravel::configureSilverstripe();
如果您使用的是Silverstripe 3.1,则MySQLDatabase::set_connection_charset('utf8');行可能不存在。这没关系。
配置数据库
如果您尚未这样做,请创建一个用于开发的数据库和用户,并在您的app/config/database.php文件中配置它,就像配置任何Laravel项目一样
return array( 'default' => 'mysql', 'connections' => array( 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'mysite', 'username' => 'mysite', 'password' => 'mysite', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), ), );
您配置的用户应具有CREATE、ALTER、INDEX、DROP权限,以便Silverstripe可以控制数据库结构。
如果您想使用除默认连接之外的连接,请参阅下面的配置部分,将包配置发布到您的项目中,然后在您的app/config/packages/themonkeys/laravel-silverstripe/database.php文件中指定要使用的连接名称(或特定于环境的文件,如app/config/packages/themonkeys/laravel-silverstripe/production/database.php)。
可选:创建Silverstripe缓存文件夹
我们建议在您的项目中创建一个文件夹,供Silverstripe用作其缓存,因为如果您使用任何类型的服务器复制(例如,用于负载均衡),那么您肯定希望Silverstripe缓存也得到复制。如果您不进行干预,Silverstripe将使用系统临时目录(例如/tmp/)。
mkdir public/silverstripe/silverstripe-cache echo *$'\n''!.gitignore' > public/silverstripe/silverstripe-cache/.gitignore printf "%s\n" 'g/silverstripe-cache/d' w q | ed public/silverstripe/.gitignore
上述脚本将创建文件夹,向该文件夹添加一个
.gitignore文件以防止文件夹内容被git检查(但允许.gitignore文件本身被检查,从而确保文件夹存在),然后从现有的public/silverstripe/.gitignore文件中删除silverstripe-cache行。
名称silverstripe-cache是特殊的,不能更改。
初始化Silverstripe数据库
安装此包后,您可以通过Laravel的artisan工具构建数据库
php artisan silverstripe:build --flush
如果您更喜欢使用Silverstripe的基于Web的方法,可以通过访问http://mysite.dev/silverstripe/dev/build?flush=1来实现。
在此结束时,您应看到消息“数据库构建完成!”
现在您需要为管理员用户设置密码,以便您可以通过CMS登录
php artisan silverstripe:password
上述命令将提示您选择用户名(Silverstripe期望您使用电子邮件地址,但如果是普通的用户名,如admin,它也可以正常工作)并输入和确认您的密码。
注意:如果命令因错误
此命令不允许在'production'环境中执行而失败,这意味着Laravel没有正确识别您的local环境。要修复此问题,请在命令行上运行hostname,并在您的app/bootstrap/start.php文件中添加一个与您的主机名匹配的规则(例如,在Mac OS X系统上通常是"*.local")。
调整页面基类中的预览URL
为了让您能够通过Laravel路由在CMS中预览页面,请将以下方法添加到您的Page类中,该类位于public/silverstripe/mysite/code/Page.php。
class Page extends SiteTree { private static $db = array( ); private static $has_one = array( ); public function PreviewLink($action = null) { $link = $this->RelativeLink($action); if (!starts_with($link, '/')) { $link = '/' . $link; } return $link; } }
如果您对路由的配置与常规配置不同,则需要更新此方法以确保CMS预览能够正确工作。
禁用Laravel的自动重定向
由于Silverstripe的URL大多以/结尾,而Laravel 4更喜欢不以/结尾的URL,这会导致重定向循环。为了解决这个问题,请注释掉(或删除)您bootstrap/start.php文件中的以下行。
$app->redirectIfTrailingSlash();
登录Silverstripe
访问http://mysite.dev/admin/,并使用您刚刚设置的用户名和密码登录。
用法
路由
此包添加了一种基于页面Silverstripe类名的Laravel路由。例如
// an ordinary Laravel route Route::get('/', 'HomeController@showWelcome'); // a Silverstripe route // matches any URL specified in the CMS with a page type (i.e. ClassName) of Page Route::get_silverstripe('Page', 'PageController@showPage'); // an ordinary Laravel POST route Route::post('/form', 'FormController@saveForm'); // a Silverstripe POST route // matches any URL specified in the CMS with a page type (i.e. ClassName) of PageWithForm and method GET Route::post_silverstripe('PageWithForm', 'PageController@saveForm');
get_、post_等前缀可以是Laravel支持的任何方法类型。
内容
如果您已将上述示例路由添加到您的routes.php文件中,则您已经可以尝试访问http://mysite.dev/about-us/和http://mysite.dev/contact-us/,因为这些页面已内置到默认的Silverstripe数据库中。如果您访问它们,则会出现错误Class PageController does not exist,因此请创建一个在app/controllers/PageController.php中。
<?php use Illuminate\Support\Facades\View; class PageController extends BaseController { public static function showPage() { return View::make('page', array( 'model' => CMS::model(), )); } }
并创建相应的app/views/page.blade.php
<!DOCTYPE html> <html lang="utf-8"> <head> <title>{{ $model->Title }}</title> <meta charset="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> {{ $model->MetaTags(false) }} </head> <body class="{{ $model->ClassName }}"> <div class="main" role="main"> <div class="inner typography line"> <div class="content-container unit size3of4 lastUnit"> <article> <h1>{{ $model->Title }}</h1> <div class="content">{{ ss($model->Content) }}</div> </article> </div> </div> </div> </body> </html>
现在访问http://mysite.dev/about-us/和http://mysite.dev/contact-us/,您会发现它们从CMS加载内容。
为了避免在每一个控制器中添加'model'视图数据,我们更倾向于使用视图组合器
App::before(function($request) { $page = CMS::model(); if ($page && $page->Exists()) { View::share('model', $page); } });
然后您的控制器方法将非常简单
<?php public static function showPage() { return View::make('page'); }
Laravel文档没有说明将视图组合器放在哪里是合理的。我们决定创建一个新的文件,app/viewcomposers.php(与filters.php一起),用于存放它们。为了使其生效,只需将以下代码添加到您的app/start/global.php文件底部即可
/* |-------------------------------------------------------------------------- | Require The View Composers File |-------------------------------------------------------------------------- | | Next we will load the view composers file for the application. This gives | us a nice separate location to store our view composers and shared view | data definitions instead of putting them all in the main routes file. | */ require app_path().'/viewcomposers.php';
ss()辅助函数
您可能已经注意到在上述app/views/page.blade.php示例文件中,我们使用了一个ss()函数来处理模型的内容。
这是必要的,因为为了使CMS更健壮,Laravel将某些事物(如站点内的链接)以中间形式存储,而不是作为完成的HTML。因此,由于我们绕过了Silverstripe的内置MVC框架,我们需要手动触发此渲染。此包提供的ss()函数可以使这个过程尽可能简单。
如果您想要对CMS创建的内容进行任何进一步的处理,ss()函数提供了一种机制,允许您这样做。只需编写\Themonkeys\Silverstripe\ContentProcessor类的实现,并将其绑定到Laravel的IoC容器中(例如在app/start/global.php中)。
App::bind('\Themonkeys\Silverstripe\ContentProcessor', 'MyContentProcessor');
内容过滤
此包中包含的Silverstripe(别名为CMS)外观可以用于加载给定URL的Silverstripe模型,但它只能做到这一点。对于更复杂的数据查询,您可以直接使用[Silverstripe数据模型API]。
如Silverstripe惯例,将根据?stage=查询字符串参数自动加载记录的预发布(草稿)或实时版本。
配置
要配置此包,您可以使用以下命令将配置文件复制到app/config/packages/themonkeys/laravel-silverstripe。
php artisan config:publish themonkeys/laravel-silverstripe
或者您可以直接在该文件夹中创建新文件,只需覆盖您需要的设置。
设置本身在配置文件中有文档说明。
贡献
由于没有正式的样式指南,请注意维护现有的编码风格。
许可
MIT许可证(版权所有)The Monkeys