Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

2/03/2014

Laravel sandwich

This morning, while reading  my favourite PHP frameworks code, it occured to me that Laravel application looks like one big, yummy sandwich. Well, that may sound weird but sometimes while coding, language concepts are presented in my mind like something that have nothing to do with programming. Or it does ?

So Laravel sandwich like every other is consists of three main parts:
  • top piece of bread (Application is called)
  • yummy layer (all good things like config, routes,controllers,models...)
  • bottom piece of bread (return what the client was asking)

When we transform these food terms into programming we got this:

//top piece
        $app = new Illuminate\Foundation\Application;

       // what was called
 public function __construct(Request $request = null)
 {
    
  $this->registerBaseBindings($request ?: $this->createNewRequest());
   
  $this->registerBaseServiceProviders();

  $this->registerBaseMiddlewares();
 }

As you can see above, app is called with provided request - which is little later registered into container. I was wondering why request is not registered as service provider - now I now . Request has to come first, before any bindings.
And that's all, belive it or not. Nothing fancy is happening here. Just some administrative tasks in the form of registering request into application. Yes, behind scene Symfony is doing a lot work while receving and preparing HTTP request, but it is not important for now.

Yummy part lives in the middle of our sandwich, and presents most delicious layer of Laravel delicacy. In fact we can say that this layer is most important in whole Laravel application. Here we can find all calls to routes, controllers, models, db - in other words it is the heart of domain layer, mean machine that has to be controlled somehow and from somewhere. If you don't understand what do I mean when I talk about "yummy layer" take a look at the content of these folders and files:
app/
        commands/
        config/
        controllers/
        database/
        lang/
        models/
        start/
        storage/
        tests/
        views/
        filters.php
        routes.php
You will be looking in the heart of nuclear reactor. That power needs some kind of control, or it will blow away everything. So let's push the red button. Or, let's eat our sandwich. We can do that with this method:

App::run();

        //in other words

 public function run(SymfonyRequest $request = null)
 {
  $request = $request ?: $this['request'];

        // calls dispatch route method
  $response = with($stack = $this->getStackedClient())->handle($request);

  $response->send();

  $stack->terminate($request, $response);
 }

Not so obvious ah ? Well I hope I will visit Laracon one day and thanks Taylor Otwell for this awesome piece of software that we enjoy somuch. Till next post, try to dig deeper into Laravel source code from App:run() point - there is a lot things inside.

I hope you like my post, and sorry for beeing too abstract sometimes.

No comments:

Post a Comment