Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-在MVC视图层中使用数据库(Laravel刀片)_Php_Mysql_Laravel_Laravel 4_Blade - Fatal编程技术网

PHP-在MVC视图层中使用数据库(Laravel刀片)

PHP-在MVC视图层中使用数据库(Laravel刀片),php,mysql,laravel,laravel-4,blade,Php,Mysql,Laravel,Laravel 4,Blade,在MVC框架中,在视图层与数据库交互通常是一个好主意吗 我用的是拉威尔4 我想在所有网站页面的顶部显示一些来自数据库的数据 我有一个main.blade.php和:@inc(“inc.header”) 如果应该,我如何以正确的方式从inc/header.php连接到数据库 我不想建立多个连接,一个在header.php,一个在我的页面控制器 我更熟悉PDO,而不是Laravel的数据库方法和ORM 任何建议都将不胜感激 编辑 朋友们对MVC和Laravel工作流给出了很好的建议和回答,但我主

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

  • 我用的是拉威尔4
  • 我想在所有网站页面的顶部显示一些来自数据库的数据
  • 我有一个
    main.blade.php
    和:
    @inc(“inc.header”)
如果应该,我如何以正确的方式从
inc/header.php
连接到数据库

我不想建立多个连接,一个在
header.php,一个在我的页面控制器

我更熟悉PDO,而不是Laravel的数据库方法和ORM

任何建议都将不胜感激

编辑

朋友们对MVC和Laravel工作流给出了很好的建议和回答,但我主要关心的还是这里

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

提前感谢:)

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

不,在视图层中不与数据库(模型)交互。MVC代表“模型-视图-控制器”,用于分离应用程序的逻辑、应用程序数据和业务规则

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

我不想建立多个连接,一个在header.php,一个在我的页面控制器

您可以创建一个主布局(包含标题),并将此布局用于应用程序的所有页面:

app/views/
    layouts/main.blade.php
    page1.blade.php
布局/main.blade.php

<html>
<head>...</head>
<body>

  <div>Your header that will be included in each of your pages</div>

  <!-- The content of the current page: -->
  @yield('body')

</body>
</html>
@extends('layouts.main')

@section('body')
  <div>The content of your page</div>
@stop
Route::get('page1', function(){

  //Fetch your data the way you prefer: SQL, Fluent or Eloquent (Laravel ORM)
  //$data = DB::select('select * from yourtable');
  $data = YourModel::all();

  //Pass your data to the view
  return View::make('page1')
    ->with('data', $data);
});
可以使用或获取数据

我建议您学习Laravel的基础知识,以便正确地创建应用程序的基础,这样以后就很容易维护了

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

不,在视图层中不与数据库(模型)交互。MVC代表“模型-视图-控制器”,用于分离应用程序的逻辑、应用程序数据和业务规则

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

我不想建立多个连接,一个在header.php,一个在我的页面控制器

您可以创建一个主布局(包含标题),并将此布局用于应用程序的所有页面:

app/views/
    layouts/main.blade.php
    page1.blade.php
布局/main.blade.php

<html>
<head>...</head>
<body>

  <div>Your header that will be included in each of your pages</div>

  <!-- The content of the current page: -->
  @yield('body')

</body>
</html>
@extends('layouts.main')

@section('body')
  <div>The content of your page</div>
@stop
Route::get('page1', function(){

  //Fetch your data the way you prefer: SQL, Fluent or Eloquent (Laravel ORM)
  //$data = DB::select('select * from yourtable');
  $data = YourModel::all();

  //Pass your data to the view
  return View::make('page1')
    ->with('data', $data);
});
可以使用或获取数据


我建议您学习Laravel的基础知识,以便正确创建应用程序的基础,以便在将来很容易维护。

除了在视图层中循环数据外,不要做任何事情。基本上,laravel中的正常MVC模式可以是这样的:

<?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');

  }

}
这一切都是从路由层开始的(顺便说一句,这在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');

});
  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');

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

上面的控制器可以是这样的:

<?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的新手,你应该看看一些关于这个主题的教程

编辑:

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

    class BaseController extends Controller {

      public function __construct()
      { 
        $data = array('alot', 'of', 'data');
        return View::share('data', $data);
      }
   }
现在,数据变量在所有视图中都可用


另外,过滤器是用来过滤东西的,比如检查某些东西是否“正常”。这可能包括检查授权用户或表单提交等。

除了在视图层中循环浏览数据外,不要做任何事情。基本上,laravel中的正常MVC模式可以是这样的:

<?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');

  }

}
这一切都是从路由层开始的(顺便说一句,这在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');

});
  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');

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

上面的控制器可以是这样的:

<?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的新手,你应该看看一些关于这个主题的教程

编辑:

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

    class BaseController extends Controller {

      public function __construct()
      { 
        $data = array('alot', 'of', 'data');
        return View::share('data', $data);
      }
   }
现在,数据变量在所有视图中都可用


另外,过滤器是用来过滤东西的,比如检查某些东西是否“正常”。这可能包括检查授权用户或表单提交等。

还有另一种解决方案,对于您这样的情况特别方便。如果您有15条路线最终都包含inc.header视图。。。好吧,你可以看到这是怎么回事。您将在多个位置重复数据逻辑。Patrik建议使用BaseController,这是一个很好的解决方案,但我更喜欢使用composer

View::composer('inc.header', function ($view)
{
    $view->some_data = MyModel::where(...)->get();
});

这并不是那么容易的事。:)

还有另一种解决方案,对于像您这样的案例特别方便。如果您有15条路线最终都包含inc.header视图。。。好吧,你可以看到这是怎么回事。您将在多个应用程序中重复数据逻辑