Php 访问Laravel上控制器中的模型的正确方法是什么
我是拉威尔4号的新手,我正在努力理解它 在谷歌和stackoverflow上搜索。也许我不是在寻找正确的语法,但我希望有人能帮助我 在CodeIgniter中,我理解它(可能)。我在控制器中使用:Php 访问Laravel上控制器中的模型的正确方法是什么,php,laravel,laravel-4,Php,Laravel,Laravel 4,我是拉威尔4号的新手,我正在努力理解它 在谷歌和stackoverflow上搜索。也许我不是在寻找正确的语法,但我希望有人能帮助我 在CodeIgniter中,我理解它(可能)。我在控制器中使用: function __construct() { $this->load->model('example_m'); } 但是在拉威尔4号怎么样 我得出以下结论: 我在de模型中创建了一个静态函数,因此我可以在任何地方访问它。例如: class Example extends Eloque
function __construct()
{ $this->load->model('example_m'); }
但是在拉威尔4号怎么样
我得出以下结论:
我在de模型中创建了一个静态函数,因此我可以在任何地方访问它。例如:
class Example extends Eloquent // this is the model
{
public static function TestExample(){
// do some stuff here
}
}
或者我可以这样做:
class ExampleController extends BaseController
{
public $test = null;
public function __construct()
{
$this->test = new Example();
}
public function index()
{
$this->test->TestExample();
}
}
我的问题是:还有其他方法和/或正确的方法是什么
您的意思是简单地访问模型的方法吗 因为它们是静态的,所以使用:model::method()
你可能必须做一个作曲家转储自动加载,虽然如此L4自动加载它正确 您的意思是简单地访问模型的方法吗 因为它们是静态的,所以使用:model::method() 你可能必须做一个作曲家转储自动加载,虽然如此L4自动加载它正确
App::bind('ExampleModelInterface', 'Example');
class ExampleController extends BaseController {
public function __construct(ExampleModelInterface $model)
{
$this->model = $model;
}
}