PHP-在MVC视图层中使用数据库(Laravel Blade) [英] PHP - Use database in MVC view layer (Laravel Blade)

查看:178
本文介绍了PHP-在MVC视图层中使用数据库(Laravel Blade)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC框架的视图层中与数据库进行交互通常是一个好主意吗?

Is it generally a good idea to interact with database in view layer in MVC frameworks?

  • 我正在使用Laravel 4.
  • 我想在所有网站页面的顶部显示数据库中的一些数据.
  • 我有一个main.blade.php和:@include("inc.header")
  • I'm using Laravel 4.
  • I want to show some data from database at top of all website's pages.
  • I have a main.blade.php and: @include("inc.header")

如果应该,如何以正确的方式从inc/header.php连接到数据库?

If I should, how can I connect to database from inc/header.php in right way?

我不想在header.php处建立多个连接,而在页面控制器中建立多个连接.

I don't want make multiple connections one here at header.php and one in my pages controllers.

我比Laravel的数据库方法和ORM更熟悉PDO.

I'm more familiar with PDO than Laravel's database methods and ORMs.

任何建议都值得赞赏!

修改

朋友们对MVC和Laravel工作流给出了很好的建议和答案,但是我主要的关注仍然在这里.

Friends gave nice advice and answers about MVC and Laravel workflow, but my main concern is still here.

好吧,我已经使用控制器和模型来获取所需的数据,然后正如我所说的,它应该出现在每个页面的视图层中,所以我应该在所有控制器的操作中重复相同的任务以获取相同的数据? (我想这就是为什么我们这里有过滤器!然后再次,可以在Laravel过滤器中使用db吗?使用模型吗?)

Ok I have used controllers and models to get required data, then as I said, it should be present for the view layer in every page, So should I repeat the same task to get the same data in All my controllers' actions? (I guess that's why we have Filters here! then again, is it ok to use db in Laravel filters? using a model?)

先谢谢您了:)

推荐答案

除了在视图层中循环遍历您的数据外,别无所求.基本上,laravel中的正常MVC模式可能是这样的:

Never do anything more than loop thru your data in your view layer. Basically a normal MVC pattern in laravel could be something like this:

这一切都始于路由层(顺便说一句,在laravel中很棒)

It all begins with the routing layer (which btw. is fantastic in laravel)

使用闭包

Route::get('/home', function()
{

 //Here data is an array, normally you would fetch data 
 //from your database here and pass it to the View.

 $data = array('this', 'is', 'my', 'data-array');
 return View::make('my.view')->with(compact('data');

});

使用控制器(和控制器方法)

//the same route is bound to a controller method
Route::get('/home','HomeController@myFunction');

上面的控制器可能看起来像这样:

The controller for the above could look something like this:

<?php

class HomeController extends BaseController {

  //The function you call from your route
  public function myFunction()
  {

     $data = array('this', 'is', 'my', 'data-array');
     return View::make('my.view')->with(compact('data');

  }

}

上面的示例仅显示了MVC中的VC,但是通常您以相同的方式传递来自模型的数据.

The above example just shows the VC in MVC, but generally you pass data from your models in the same way.

这里有个快速提示:

控制器中的模型用法

  public function myFunction($user)
  {

     $userdata = User::find($user)->orderBy('firstname', 'desc');
     $infodata = Event::find(1)->course;
     return View::make('my.view')->with(compact('data', 'infodata');

  }

因此,想法是laravel可让您以多种方式做事.如果您的应用程序较小,并且确保无需控制器即可进行管理,则可以跳过控制器并将所有内容保留在路由层中.

So the idea is that laravel lets you do things in multiple ways. If you have a minor app, and are sure that you can manage without controllers you could skip the controller and keep everything in your routing layer.

但是对于大多数应用程序而言,需要控制器来控制应用程序中的数据流.

For most apps however the controllers are needed for controlling the dataflow in the application.

如果您是MVC的新手,则应查看有关该主题的一些文章.

If you are totally new to MVC you should check out some tuts on the subject.

啊哈!因此,您想在所有视图中共享一些数据!嗯,那很简单.因为您所有的控制器都扩展了BaseController,所以您只需在其中传递数据即可.像这样:

Ahaa! So you wanted to share some piece of data in all your views! Well thats simple. Because all your controllers extend the BaseController you can simply pass in the data in there. Like so:

    class BaseController extends Controller {

      public function __construct()
      { 
        $data = array('alot', 'of', 'data');
        return View::share('data', $data);
      }
   }

现在,数据变量在所有视图中都可用.

Now the data variable is available in all the views.

PS.过滤器旨在过滤东西,例如检查某些东西是否正常".这可能包括检查已验证的用户或表单提交等.

PS. Filters are meant to filter stuff, like to check if certain things are "ok". This could include to check authed users, or form submissions etc.

这篇关于PHP-在MVC视图层中使用数据库(Laravel Blade)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆