CategoryLaravel

Helper classess in laravel

      In most case, we want to extend our Laravel applications capabilities by adding helper class.
It is not a best practice to insert this classess inside Controller and Model.

so we have to write our classess in separate files and group it by folder

Here let us have a look how we add helper classess in laravel

1.    We have app folder on root folder of laravel.There we can create a Libraries folder inside app folder.

2.  We can create a class/helper file and we can write a our custom class there, on the top of custom class we have to define namespace our_folder_name;




 

namespace our_folder_name

for example ,

 
//here my folder name is Libraries
namespace Libraries;

class className{
    
    //class methodes
    
}

Now we can go to our controller and on top we can include our helper class

use Libraries\filename as filename;

And we can go to compser, inside autoloder we can add “app/foldername”

app/foldername

 

"autoload": {
		"classmap": [
			"app/commands",
			"app/controllers",
			"app/models",
			"app/database/migrations",
			"app/database/seeds",
			"app/tests/TestCase.php",
 			"app/Libraries",// we've added classes folder on compose
		]
	},

 




And we can update composer by using below comment

 composer dump

Now we can use our helper class in our controller

  $test = new className();

All my samples files are given below
Library/className.php

<?php
 namespace Libraries;

class className{
    
    //class methodes
    
    function testmesthode(){
        
        return "Hello world";
    }
    
}

testController.php

<?php

use Libraries\className as className;

class testController extends BaseController {
    
    
    function index(){
        
        $test = new className();
        echo $test->testmesthode();

    }
    
}

compser file

"autoload": {
		"classmap": [
			"app/commands",
			"app/controllers",
			"app/models",
			"app/database/migrations",
			"app/database/seeds",
			"app/tests/TestCase.php",
 			"app/Libraries",// we've added classes folder on Laravel ClassLoader
		]
	},



1,385 total views, 1 views today

Global Config values In Laravel

Today I want to create a page for sending contact us mail in site,

For that I have to define admin mail id and admin name as a config value and i want to access the same in same in many  places,

I had googled for some time , to find out how can i define my constant in laravel and one of my friend (Ranjith) help to do that ,

Here i am writing how we can use a  Config values   in laravel,

1. If we want to define all our constants in a custom page ,

We can create a page (Here i am calling constants.php )
and inside that we can define like key value pair

return array(
 'custome_name' =>'custome_value',
)

And we call the values as below

$cvalue = Config::get('filename.custome_name');

Here we will get our custom_value in $cvalue

Let us look an example

<?php

return array(

    'admin_email' =>'mail@shabeebk.com',
    'admin_name' =>'Admin',
  

);
?>

Now let us see How we can retrieve the config in your code somewhere

echo  Config::get('constants.admin_email');
echo  Config::get('constants.admin_name')

Hope it will help some one 🙂

Thank you Ranjith

I made small changes,

thank you Martin Tonev for correcting from variable to config

9,905 total views, 3 views today

Install Constant Contact in Laravel

To setup an option for newsletter signup with Constant Contact and Laravel Framework

  1. Install PHP5 Curl using the below command
    sudo apt-get install php5-curl
    
  2. Add the below code in composer.json
    "require": {
               "laravel/framework": "4.2.*",
               "curl/curl": "1.2.*",
               "constantcontact/constantcontact": "1.2.*"
    	},
    
  3. Run the below command to install constant contact in laravel
    composer update
    

     

1,303 total views, no views today

How to install Laravel in MAMP-MAC

Today i was trying to install laravel in MAMP , While going through their step on laravel site mentioned i found that i didn installed mycript extension. I had googled for lot of time and lastly i installed .

I am writing this will help some one to install laravel in Mac using MAMP.

Install composer

Before installing laravel we have to install composer . We can go to https://getcomposer.org/ and we can install from from there or we can follow the following steps step

1. first we have to check curl is enabled . step 2. we can install composer globally as mentioned in their site

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Install Laravel 

1.once we installed composer we have to move to htdocs and to our directory from our terminal

cd /directory/folder

2. We can tell composer to download laravel  and install by following commands

composer global require "laravel/installer=~1.1"
composer create-project laravel/laravel your-project-name --prefer-dist

And here it will come to as for mycrypt extension .

Mcrypt PHP extension required.

To solve this issue we have to check the php version frist

php -v

shabeebk.com-php-version-check

   3.Now let us have our PHP version, in my case 5.6.2, we can open our .bash_profile script

nano .bash_profile

4. Add our MAMP PHP version after the code already present ,Exit the file and Save   it.

export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.10/bin
export PATH="$MAMP_PHP:$PATH"

5. Now we can try to install Laravel  again That is it!.

composer create-project laravel/laravel your-project-name --prefer-dist

 

6. We can see Laravel is downloading and installing

laraval-install-mac_1

laraval-install-2

laraval-install-3

laraval-install-4

  Finally it will show like installed. Application key (*******) installed successfully.

 

Now let us go to browser and check the URL.

Screen Shot 2014-12-25 at 9.56.18 pm

That is it!

 

 

 

6,101 total views, 1 views today

© 2017 shabeeb Blog

Theme by Anders NorénUp ↑