Use laravel blade template out side of laravel

Php is a very nice platform and programming langauge for building web application. To build web application we have to build dynamic html using php tags and have to process data. But presenting html is a different construct and processing data is a different construct. So if we would like to separete these both construct in php we have to use template system. Laravel provides awesome template engine but if we do not have to use laravel but it’s blade template engine we can.

We just have to add below dependency in our project composer.json file

Link for laravel blade template php packe: https://packagist.org/packages/philo/laravel-blade

"require":
      {
        "philo/laravel-blade": "3.*"
      },

Than we have to install it via below command

$ > composer install 
or 
$ > composerupdate

Once php package for blade template engine is installed let’s see how we can use it.

Create cache and view folders inside your app directory.

Now let’s create one example template view file inside views directory.

Let’s use this template to show data pass from php.

This is how we can use an awesome laravel blade template engine out side of laravel in php project.

Advertisements

Conditionally include CSRF token verification in laravel 5 routes

While developing web application in laravel 5 you find that it always require csrf token for every request. I mean when ever you create form in your view you always have to add token as hidden input field. Also you have to mentioned this csrf token while you are making ajax request.
For example

	//In FORM
	<form method="post" action={{some_action"}}>
	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	.....
		Other form stuff
	....
	</form>

And in ajax

	
	//In Ajax request
	
	$.ajax({
        url: {{ route('route_name')}},
		type: "post",
    	dataType: 'json',
		data: { param1: value, param2:  value, _token: {{ csrf_token() }} },

		success:function(data){
			//success callback
		}
	});

Continue reading “Conditionally include CSRF token verification in laravel 5 routes”

Laravel 5.1 Pass middleware parameters as array

Laravel has added support for middleware parameter since version 5.1 . So it is nice that we can pass middleware parameter from Routes.php or from controller. Let’s see an example.


Route::get('test',['middleware'=>'hasAccess:Admin', function(){
 return "Access granted for this operation"; 
}]);

or in controller constructor

//Other stuff in controller
public function __construct()

{

    $this->middleware('hasAccess:admin');

}

//Other stuff in controller

and in middleware handle function you can catch argument which is passed as mentioned below:
Continue reading “Laravel 5.1 Pass middleware parameters as array”

Laravel: Form-validation (A server side concern)

In Laravel up till now we are familiar with model-view-controller components , some routes and awesome blade syntax to decorate the views and write easy and short syntax for displaying data provided by the controllers. Now when we are talking about the application in day to day use, it will accept some data from users. In web-application data is collected using in html web-pages. form element have several other child where user can enter data and submit. Once user submit data it’s application’s responsibility to check whether submitted data is correct according to business rule or not. So data should be validated from application side before it stored in database or any persistence system.
To validate the data if i am writing a program in core PHP so i have to use empty, isset etc.. built in PHP functions and some custom logic with conditions according to requirements. But Laravel provides nice way to validate the data. Few methods and pass rules array to that methods. Let’s see an example.
Let you have a sign-up form for registering users to your application. So when user clicks on Sign Up links in your application, user will see below form.
Screenshot from 2015-01-12 14:48:29
Continue reading “Laravel: Form-validation (A server side concern)”

Blade: Laravel inbuilt template engine

laravel_blad

Introduction

PHP is getting popularity due to easy to learn and easy to use. It can be easily understood code by looking at php file that have logic inside php tags and printing html based on data that php have. It is fine with embedded php inside html when there are few pages in web application. But if it is a latger application than it is necessary to extract logic in seperate php file and pass the data in another php file where php tags are used just for iteration, condition and printing data. We can achieve this by using template engine. There are numbers of template engines available in php but in larave you can find blade template engine which is powerful and easy to use template engine.

A template engine is nothing but provide facility to write short syntax in our php file where all presentation logic recides. It also provide to decalre common layout so that other pages can use it. You do not have to install blade template engine separately in you laravel project. It is already installed along with laravel. In this post we will briefly see it’s features in brief.
Continue reading “Blade: Laravel inbuilt template engine”

Laravel MVC

Well now you have some rough idea for how to create basic site having 4 to 5 pages in Laravel after learning laravel routes. Now we will go ahead and learn real programming in laravel framework by utilizing it’s capabilities. You heard the word “MVC” since you have started developing the web-application and have eager to know:

  • what MVC stands for?
  • how i can use it in my programming approach?
  • How can i benefited by using it?

Well MVC’s full form Model View Controller. It’s not a programming language specific, but it’s design pattern and used in many programming languages like Java, C# and in PHP of course because we are looking and interested for the same in this blog post.
MVC

Responsibilities of each components in MVC are:

Laravel routes( routes.php)

IMG_4620

In previous post we understand the directory structure of laravel application, so we have some basic idea about where different application components reside. Before we go deeper and start architectural components like model, view and controller, lets understand routes.php. As routes.php file gives you a hint for each request which controller and which method inside that controller will execute.

Continue reading “Laravel routes( routes.php)”