How to Make Use of App::Make() In Laravel 4 for Rendering Views

Feb 18, 2015 3:14:46 PM

Getting stuff done with Laravel 4 using the App::make() method is extremely safe and rather a better approach to decouple dependencies from each other. While you type hint a dependency in the constructor method, the Laravel framework automatically injects dependencies by reading the type of its dependent object while using App::make() behind the scene.

Problem - How to call a controller in Laravel 4 view blade file

While working on an application in Laravel 4, we were facing a problem in rendering one view from another view and also within that view. Each view is containing different data to show and the view should be rendered without page reload.

Solution Proposed By Daffodil Team

We implemented App::make() method in laravel which turned out to be very useful. On calling App::make() method, the Closure callback is executed and the result is returned.

Let me illustrate this using an example

Suppose you have a view blade say "one.blade.php" and you have bootstrap tabs. Every time the tab is clicked, you want to render same/different view with different data without reloading the page.
Shared below is the code that we used to solve the problem -

In the file one.blade.php, insert the code -


[php]<ul class="nav nav-tabs" id="nav-tabs" role="tablist">
<li class="active"><a href="#newdata"> NewData </a></li>


[php]<div class="tab-pane active" id="newdata">


Now at the time when one.blade.php got rendered, at the same time App::make() under the NewData tab gets executed and result is returned in a string format. Now we just have to echo it to render the view with different data.

Similarly, you can also try the solution to implement App::make() manually for rendering a view within that view.

In your controller (Your_controller_name), you have to create a function by the name getData, like
public function getData () {

and do whatever calculations you want to do. At the end you can insert the
return View::make() code to render a view file with data in the portion of that tab onto the one.blade.php.

The view will render without having any route and without reloading the page.

One can also include view using @include() method also. Although this method is different from App::make() as with App::make(), we can render the same view with different data which is otherwise not possible with @include() method.

We hope you enjoyed reading the blog!!

Happy coding!!

Topics: Web Development

Team Daffodil

Written by Team Daffodil

We equip smart companies with smarter solutions. Our all-round expertise in Web, Mobile and Cloud solutions has brought about a change in some of the top businesses, setting global milestones as we go.