我正在使用Laravel 4构建CMS,我有一个管理页面的基本管理控制器,如下所示:
class AdminController extends BaseController {
public function __construct(UserAuthInterface $auth, MessagesInterface $message, ModuleManagerInterface $module)
{
$this->auth = $auth;
$this->user = $this->auth->adminLoggedIn();
$this->message = $message;
$this->module = $module;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用Laravel的IOC容器将类依赖项注入构造函数.然后,我有各种控制器类来控制组成CMS的不同模块,每个类都扩展了管理类.例如:
class UsersController extends AdminController {
public function home()
{
if (!$this->user)
{
return Redirect::route('admin.login');
}
$messages = $this->message->getMessages();
return View::make('users::home', compact('messages'));
}
}
Run Code Online (Sandbox Code Playgroud)
现在这完全可行,但是当我向UsersController类中添加构造函数时,我的问题就出现了问题,这不是问题,而是更多的效率问题.例如:
class UsersController extends AdminController {
public function __construct(UsersManager $user)
{
$this->users = $users;
}
public function home()
{
if (!$this->user)
{
return Redirect::route('admin.login'); …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个基于Laravel 4框架的cms.我正在尝试构建一个类似于Pyro CMS的插件系统,其中模块视图可以使用Blade模板系统包含在页面视图中.
我正在构建一个联系表单插件,如果成功提交,则会将用户重定向到给定的URL,或者只是重定向回现有页面.
我的联系表单类的代码是:
class Contact {
public static function form($params)
{
//get params and execute relevant logic here
$redirect = isset($params['redirect']) ? $params['redirect'] : Request::url();
$data = Input::all();
if($data)
{
// Run validation and send message here
return Redirect::to($redirect)
}
return View::make('contact_form_view');
}
}
Run Code Online (Sandbox Code Playgroud)
有一个页面控制器将根据使用的路径显示相应的页面视图,并且想法是用户可以将联系表单拖放到任何页面模板中,并通过在模板视图中调用表单功能轻松地自定义它,如下所示
<html>
<head>
</head>
<body>
{{ Contact::form(array(
'to' => 'myemail@mydomain.com',
'view' => 'contact_form_1',
)) }}
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
除了重定向之外,这一切都很好.当表单成功提交并且发送的消息页面刷新并显示以下消息代替联系表单
HTTP/1.0 302 Found Cache-Control: no-cache Date: Tue, 17 Sep 2013 09:14:16 GMT Location: http://localhost:8888/my_initial_route Redirecting to …Run Code Online (Sandbox Code Playgroud)