Laravel 4: how can I understand how it all works?

蓝咒 提交于 2019-12-03 13:03:31

问题


I am using Laravel 3 in one project and it's been a joy. I have also looked at the source code several times to see how some things work behind the scenes.

But now in Laravel 4, I don't know where to begin or how to understand it all. Where can I learn all the behind the scenes of Laravel 4?

Case in point: I wanted to find out if the DB::insert() returns the id of inserted row. So I started searching. 1. I found the Illuminate\Support\Facades\Facade class that "encapsulates" DB. 2. The resolveFacadeInstance function is called and then I tried to print these arrays, but my computer hangs :-/. And I'm sure this would lead to many more classes that I wouldn't understand.

Is there a way I could try to learn the inner workings of Laravel 4? Maybe stack traces?


回答1:


The facade class is just a filter class to allow you to call methods as if they were static. For the facade mappings go here: http://laravel.com/docs/facades#facade-class-reference

The starting point to fully understand laravel's inner-workings should begin at:

/public/index.php

You can follow the logic of the program, noticing that requires start.php, which loads an instance of the "Application" which is found here:

/vendor/laravel/framework/src/Illuminate/Foundation/Application.php



回答2:


This Tuts+ video shows a couple of ways of finding out what class is actually doing the work.

E.g.:

$root = get_class(DB::getFacadeRoot());
var_dump($root);



回答3:


You can check out the early docs for Laravel 4 here : http://four.laravel.com/ – that should give you a good starting point




回答4:


The actual Laravel 4 code is well documented in the files. If you want to understand the inner workings then open up the source code files and read the notes. For example I looked up the DB::insert() code in /vendor/laravel/framework/src/Illuminate/Foundation/Application.php.

/**
 * Run an insert statement against the database.
 *
 * @param  string  $query
 * @param  array   $bindings
 * @return bool
 */
public function insert($query, $bindings = array())
{
    return $this->statement($query, $bindings);
}

Ok, so this is calling the statement function so I search for function statement in the same code / class:

/**
 * Execute an SQL statement and return the boolean result.
 *
 * @param  string  $query
 * @param  array   $bindings
 * @return bool
 */
public function statement($query, $bindings = array())
{
    return $this->run($query, $bindings, function($me, $query, $bindings)
    {
        if ($me->pretending()) return true;

        $bindings = $me->prepareBindings($bindings);

        return $me->getPdo()->prepare($query)->execute($bindings);
    });
}

We can now see that this returns the boolean result based on the comments above the code.




回答5:


If you come from Laravel 3 this article is for you. After that you should read the other tutorials of that series.

Author's note:

This article should outline some of the more important changes to Laravel between versions 3 and the upcoming version 4. Bear in mind this isn’t all of the changes. As the release of Laravel 4 gets closer I’ll keep this article up to date. If you’re having any problems with Laravel 4 please jump on to #laravel on Freenode. At this time we’d like to ask people not to post help topics on the forums.



来源:https://stackoverflow.com/questions/14348100/laravel-4-how-can-i-understand-how-it-all-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!