আগামী ৩০ অক্টোবর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML,CSS,Bootstrap,PHP,MySQl, AJAX and JQUERY কোর্সের ৮৬ তম ব্যাচ। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722817591, Email : [email protected] কোর্সের সিলেবাস এর জন্য এখানে ক্লিক করুন ।

Getting Started With Laravel

Hi guys! In this tutorial of Laravel we’re going to learn the controlling system, routing system & blade template engine system. I’m going to give you some basic idea’s of creating controllers & routing through your application & give your application layout the ultimate & easier templating system which is blade.

Now that we know about the Laravel’s file structure, we’ve taught it about it in the earlier post. Those who missed it please go to this tutorial of file structure of Laravel. It’s time for to start what we’re here for. We’ll discuss about these things in three different portion.

1.  Installing & Configuration:

First off all download a fresh copy of Laravel from it’s website. Now put it into your web server or local server what you wish for. I use wamp server that’s why i’ve put it on my www directory. For testing please check that your downloaded copy works fine. Go to “localhost/yourfoldernameoflaravel/public” & check that if it’s running fine. Let’s see the picture below:

fresh start

Now we’re going to do something here with Artisan the command interface that we’ve talked earlier. For that we’ve to open command prompt. Before doing something in the command prompt we’ve to make sure that php keyword is known by your command prompt & for that please note that where you installed your php version. For me i’ve installed my php on “D:\wamp\bin\php\php5.4.3“. So we’ve to put this path to environment. Now right click on my computer & click properties. Then go to “Advance System Settings” tab and a pop up window will appear. In this window go to “Environment Variables” another pop up window opens & in this window in “System Variables” find the variable named path click on edit & put the path of your php installation in there in the end of line like this “ ;D:\wamp\bin\php\php5.4.3” this semicolon is for the end of the other line in front of your path don’t forgot to put semicolon in front of your path. Please check the image below to be sure about the process.

env var setup

Now as we’ve made the command prompt to know the php command now it’s time for the configuration. First go to the config folder & open application.php file. In here we’ve to set these things like first off all set the “Application URL” which is your base url see the first picture where it says it’s your base url. Just copy the url from your browser & put that into the url variable. Just after paste your url cut the front slash at the end of the public ok that’s what Laravel told us to do.

'url' => 'http://localhost/laravel/public',

Now then it’s time for index variable you can see that there is written down index.php what we don’t want in the browser. We would like to visit in the browser without written down this index.php thing for having relief of it what we should do is just delete this index.php & active your mod_rewrite option. For activating your mod_rewrite option in wamp left click on the icon of wamp a pop up menu appears go to apache/httpd.conf in there just find mod_rewrite & uncomment it by deleting the “# or ;” character. Ok we’re done. Now for the “Application Key“. This key is used by the encryption and cookie classes to generate secure encrypted strings & hashes. What we’ve to do is remove what first says in the ‘key’ option & save it. Then open command prompt redirect to the directory where you saved your application module means where you saved Laravel. Now do what the picture told you below:

key generate

Now as you can see first off all you put blank line in the ‘key’ var & save it. After the command a key has been generated automatically & now save it. Your key should almost look like this with different hashing:

'key' => 'nkzDTu07Z47bgY8IgvsMsrXhHmHOlfCF'

Now let’s go to the database.php file, here we can see that everything is given what we’ve to do is choose which DB we’re going to use. For default one i’m going to use ‘MYSQL’ for that. In connections just see the below code for the example & put yours like this for the connectivity for the DB.

'connections' => array(

 

'sqlite' => array(

'driver'   => 'sqlite',

'database' => 'application',

'prefix'   => '',

),

 

'mysql' => array(

'driver'   => 'mysql',

'host'     => '127.0.0.1',

'database' => 'laravel',

'username' => 'root',

'password' => '',

'charset'  => 'utf8',

'prefix'   => '',

),

 

'pgsql' => array(

'driver'   => 'pgsql',

'host'     => '127.0.0.1',

'database' => 'database',

'username' => 'root',

'password' => '',

'charset'  => 'utf8',

'prefix'   => '',

'schema'   => 'public',

),

 

'sqlsrv' => array(

'driver'   => 'sqlsrv',

'host'     => '127.0.0.1',

'database' => 'database',

'username' => 'root',

'password' => '',

'prefix'   => '',

),

 

),

In here i’ve used mysql part that’s why i just put the connectivity settings in mysql array. It’s the end of the database connectivity. It’s also finishing of installing & configuration part. Let’s proceed to another part.

1.  Controlling, Routing & Viewing:

Now it’s time for to do something with our application. We’ll go from the very beginning stage as this tutorial’s about the beginners to learn Laravel. So now first off all go to the controller folder & create a file named authors.php & paste the code below:

class Authors_Controller extends Base_Controller{

public $restful = true;

public function get_index(){

return View::make('authors.index');

}

}

Here we create a class name Authors_Controller which extends Base_Controller. Now what we tried to explain in here is that with the class name you’ve to put an postfix name “_Controller” then extend the class the base controller. This “Base_Controller” also extends the main “Controller” class you can view it on base.php file under the controller class. Now as we can in the above code we declare a public function get_index() because we’ve to put a prefix before all the methods name it’s define by the Laravel why we should give a prefix is that so that “HTTP” would find out what this method is like getting something or posting somethine like the GET & POST method in php. Actually we should put “action_” prefix before the method name for knowing what action it is but we don’t want to use action all the time that’s why i’ve put a public property called $restful & made it true so that we can use any other prefix ok. Now in the method we’ve returned a class name View which has a static method called make & in here we would throw the view elements first the folder name than the file name so in the upper code first folder name will be authors & file name is index.php.

Now as we’ve created our controller it’s time for the view. Go to the view folder create a folder named authors & inside that folder create a file named index.php as we’ve told the controller method to return to this file structure of view. Open the index.php file & paste the general html code like the code below:

<h1>Hello World</h1>

Now it’s time for routing as we’ve created our controller & view file we should do what is told the routes to where to go to or back to. Now go to the routes.php file here a default routing system is given for the first page as we’ve seen at the beginning when we run laravel in our server.

Route::get('/', function()

{

return View::make('home.index');

});

In here Route has actually many static methods. Every method has two parameter first one is ‘/’ this actually it means the url that you want to visit & the second parameter can be an array or function. The default routing system uses a function but we’ll use an array for learning & an array suits best actually. Now paste the copy below so that we can run our view in the server via routing.

Route::get('authors', array('uses'=>'[email protected]'));

In here we are giving the first parameter the url & in the second parameter we’re using an array. In this section we have to told the router to where we want to go like the class name & the action name for that we’ve to use some built in pair of keys. For this instance i used the ‘uses’ key in array and then the class name after it an @ sign and then the action name which is index in our controller.  Now go to the browser write down the address as “localhost/laravel/public/authors” here authors is the controller name & check if the page is loading like the picture below:

first routing

If everything’s working we could see our first laravel page as the picture above. Yes in this section of article we’ve created our first controller, view & routes & also we’re sure to do that correctly. Now you can say that you can create a basic page in Laravel also isn’t it?

Now it’s time for our blade template engine learning. For that we’ve to do a little more customization in the controller nothing bigger just put some info nothing else. For this go to the controller folder & open authors.php again & paste the code below:

<?php

class Authors_Controller extends Base_Controller{

public $restful = true;

public function get_index(){

$view = View::make('authors.index',array('name'=>'Adnan'))->with('age','24');

$view->location = 'Dhaka';

$view['speciality'] = 'PHP';

$view->title = 'Authors & books from Controller';

return $view;

}

}

Here what we can see is giving some info of mine. In make method we can give the view file destination & with that we can also give some info through an array like I did in the upper code. Here you can get confused with some code like “$view->location or $view[‘speciality’]” let me tell you one thing when we make this kind of things these will use as the variables within the $view var in the view page, how?? we’re going to learn this in short time.

Now go to the view directory and open “authors/index.php” & in there just paste the code below:

<h1>Hello World</h1>

&nbsp;

<?php echo $name; ?><br>

<?php echo $age; ?><br>

<?php echo $location; ?><br>

<?php echo $speciality; ?><br>

This is the normal way of posting the data from the controller in the view in php. Now go to the browser & see if the code is running well, if yes then it should look like the picture below:

normal php form design

But what we want to do is code in blade template engine. If we use blade template engine it’ll be much better for everything the code will not be messi.

Now for Blade Template Engine you’ve to rename the index.php to index.blade.php, why blade because Laravel needs to understand the blade coding from the file that’s why we need to name the file .blade.php ok. Now in the above code what we can change is see the code below:

<h1>Hello World</h1>

{{$name}}<br>

{{$age}}<br>

{{$location}}<br>

{{$speciality}}<br>

As you can see here in the code I just delete the php tag in here just named the variable & put the curly braces around the variable that’s the blade engine about replacing php syntaxes with these bunch of li’l code. I will brief to you with another example also but before that go check that if anythings broken or not.

Now it’s time for some implementation on blade engine with more specifications. For that we need to do some extra works. We are gonna set a layout so for this copy the code from below & paste it into your index.blade.php file.

@layout('layouts.default')

&nbsp;

&nbsp;

@section('content')

<h1>Hello World</h1>

@if(isset($name))

{{$name}}<br>

@else

No name given<br>

@endif

&nbsp;

{{$age}}<br>

{{$location}}<br>

{{$speciality}}<br>

@endsection

Now as we’ve create a layout file & called it in the upper section like this “@layout(‘layouts.default’)”. What we can understand in here is @ sign is used by the blade engine layout is method in there in this method we calling the file name with “default” from the folder “layouts”. And in layouts file we are telling them to what content it should fetch from the index.blade.php file & we’re giving the content by this method called “@section(‘content’)……@endsection“. So that we’ve told the layout to fetch the info so we need to create a layouts folder in view folder & inside that a file named default.blade.php. In default.blade.php file we’re gonna put some code just copy it from below:

<!DOCTYPE html>

&nbsp;

<html>

<head>

<title>{{ $title }}</title>

</head>

<body>

@yield('content')

</body>

</html>

This is a simple HTML code & inside that we’ve implemented blade coding. Here we’ve put “{{ $title }}” remember in the controller we’ve created a variable like this “$view->title = ‘Authors & books from Controller’;” it’s for we can dynamically set the page title ok. Now what are the works of “@yield”. It’s a method of blade template engine what we’ve created in the index file @section it’ll fetch the info in here. Now just see the picture if it’s running or not. If yes than it’s working correctly. And by this section we’ve reached to the end of this tutorial.

It’s a big article actually. It took all my day to finish this tutorial. Hope that all the viewers will like it specially the beginners one. If there is any wrong thing please tell me because it’s a huge one so may be there is some fault. If you liked the tutorial please do make comment so that we can go longer & also stay tuned for more amazing tutorials on Laravel.

If you guys want to download the source code please feel free to download from here.

An enthusiastic of work with over 2+ years of experience. Extensively experienced on wordpress, Codeigniter, Laravel. ##Visit my github page (https://github.com/ashawkat) ##Visit my blogging page where I spread out my knowledges around all other people (http://w3programmers.com/author/tanim) If you want to hire me then you can contact me via email or skype. Skype: ashawkat89 Email: [email protected]
One comment on “Getting Started With Laravel

Leave a Reply

Your email address will not be published. Required fields are marked *