Friday, January 30, 2015

Best Way of deploy in php Laravel application

Deploy in Laravel:

The Best & Easy way for deploying in Laravel is through making a method.

Step 1.

Open up your routes.php page in your /app folder, and append the following line of PHP to the file.

Route::get('/deploy', 'Server@deploy');
Step 2. 
Now go to /app/controllers and make a controller file named Server.php

and add there

class Server extends BaseController {

    public function deploy() {
    
    }
}

Step 3.
Here we can write in this method, all the terminal command which we want to run on server

SSH::into('production')->run(array(
    'cd ~/public_html/mywebsite',
    'git pull origin master'
), function($line){
    echo $line.PHP_EOL; // outputs server feedback
});
Step 4.

Now go to /app/config/remote.php And put your server information here

    'connections' => array(
        'production' => array(
            'host'      => 'hostname',
            'username'  => 'deploy',
            'password'  => '',
            'key'       => '/home/ashish/.ssh/jan20_new',
            'keyphrase' => '',
            'root'      => '/var/www/html',
        ),
    ),

All Done.

Now you can deploy by clicking this http://localhost/deploy

Friday, December 19, 2014

Uses of Indexes in mysql

Uses of Indexes in mysql:

A database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns, providing the basis for both rapid random lookups and efficient ordering of access to records.

While creating index, it should be considered that what are the columns which will be used to make SQL queries and create one or more indexes on those columns.

INSERT and UPDATE statements take more time on tables having indexes where as SELECT statements become fast on those tables. The reason is that while doing insert or update, database need to insert or update index values as well.

For adding index in table :

CREATE UNIQUE INDEX index_name
ON table_name ( column1, column2,...);

You can use one or more columns to create an index. For example, we can create an index on tutorials_tbl using tutorial_author.

CREATE UNIQUE INDEX AUTHOR_INDEX
ON tutorials_tbl (tutorial_author)

You can create a simple index on a table. Just omit UNIQUE keyword from the query to create simple index. Simple index allows duplicate values in a table.

If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name.

mysql> CREATE UNIQUE INDEX AUTHOR_INDEX
ON tutorials_tbl (tutorial_author DESC)


Thanks

Friday, December 5, 2014

Completely Login your site using Facebook, Here Backend - Laravel(php) & Frontend - Ionic(for mobile app)

Completely Login through Facebook:

Hi all, Today i am here for writing an article which is used for login your mobile app through Facebook.

Here, i am doing this using APIs. So, my backend is Laravel Framework(PHP)  & my frontend is Ionic Framework(mobile app).

Backend:

Just write down these function after your login function in your model .

    public function fbLogin(){
        $input = Input::all();          //getting all data(name,email,fb_id) from frontend
        $email = $input['email'];
        $fbId = $input['fb_id'];
        $firstName = $input['first_name'];
        $lastName = $input['last_name'];

        $user = DB::table('users')
            ->where('username', $email)
            ->orWhere('fb_id', $fbId)
            ->get();
        $noOfRow = count($user);   //getting data from table w.r.t. email




//in case where fb_id & email are exists in DB & same as getting data  row will show 1, in case fb_id $email exists in DB but not both are same then case =2, in case when both does not exists case = 0
      if($noOfRow==1){
            DB::table('users')->where('username', $email)->update(array('fb_id' => $fbId));      
            $userID = $user[0]->id;
        }elseif($noOfRow==2){
            foreach($user as $v){
                if($v->fb_id==$fbId){
                    $userID = $v->id;
                }
            }
        }else{
            if($email){
               return $this->doLoginforFB($email,$fbId, $firstName,$lastName );
            }else{
                $timeStamp = md5(microtime());
                $dummyEmail = $timeStamp.'@dummy.com'; //creating dummy email
                return $this->doLoginforFB($dummyEmail,$fbId, $firstName,$lastName );
            }
        }
         return $this->getUsersDetailsById($userID);
    }

    public function doLoginforFB($email,$fbId, $firstName,$lastName ){
        $password = mt_rand(10000000, 99999999);  //creating a dummy password
        $hashPass = Hash::make($password);

        $newUser = new User();
        $newUser->username = $email;
        $newUser->password = $hashPass;
        $newUser->fb_id = $fbId;
        $newUser->save();

        $this->RegisterStep($newUser->id);

        DB::table('users_details')->insert(
            array('firstname' => $firstName, 'lastname' => $lastName, 'user_id'=>$newUser->id)
        );

       
        $input['username'] = $email;
        $input['password'] = $password;

        Input::replace($input);

        $request = Request::create('oauth/login', 'POST');  //laravel way for passing parms in doLogin function or you can call here your Login function
        $response = Route::dispatch($request);
        return $response;
    }













Frontend:

Accordingly using this i have created my Frontend.

Just follow its steps and get all the details of any user like email, fb_id , name and send it to your backend.

After that make a build and check it out if its working

Thanks
Ashish Ginotra

Friday, November 21, 2014

Easy way to transfer payment from one account to another using STRIPE in php

Transfer payment from one account to another using STRIPE:


This is done by the following way:

1. Deduct the amount from customer using main account of Stripe.

2. Add amount to the merchant using the same main Stripe account.

For this you will be in need of a Stripe testing/working account.

Firstly Register yourself at Stripe.com

and add stripe in your project it will generate a key & configure it in your account

Then Deduct money from any account using this


Stripe_Charge::create(array(
  "amount" => 600,
  "currency" => "usd",
   "card" => array(
    "number" => "4242424242424242",
    "exp_month" => 10,
    "exp_year" => 2015,
    "cvc" => "314"
  ),
  "metadata" => array("order_id" => "6735")
));



 It will deduct $6 using this card and will be add to Stripe Account.

Now pay amount to merchant account using this

            $bank_acc = Stripe_Token::create(array( "bank_account" => array( "country" => "US", "routing_number" => "bank_routing_number", "account_number" => "bank_account_no" ) ));

            $recipient = Stripe_Recipient::create(array( "name" => "name", "type" => "individual", "bank_account"=>$bank_acc->id ));


            $payment =  Stripe_Transfer::create(array( "amount" => '600', "currency" => "usd", "recipient" => $recipient->id, "description" => "Transfer for test@example.com" ));








It will add $6 to merchant account.

Thanks

Friday, November 14, 2014

Locking Data in Laravel

Locking Data:

There are two mechanisms for locking data in a database.

1. Pessimistic locking

2. Optimistic locking

In pessimistic locking a record or page is locked immediately when the lock is requested, while in an optimistic lock the record or page is only locked when the changes made to that record are updated.

The latter situation is only appropriate when there is less chance of someone needing to access the record while it is locked; otherwise it cannot be certain that the update will succeed because the attempt to update the record will fail if another user updates the record first.

With pessimistic locking it is guaranteed that the record will be updated.

The query builder includes a few functions to help you do "pessimistic locking" on your SELECT statements.

To run the SELECT statement with a "shared lock", you may use the sharedLock method on a query:


DB::table('users')->where('votes', '>', 100)->sharedLock()->get();


To "lock for update" on a SELECT statement, you may use the lockForUpdate method on a query:

DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();



Thanks

Friday, November 7, 2014

How to send email through terminal in ubuntu?

Send Email through Terminal:

For send a email through terminal firstly install the postfix using this

sudo apt-get install postfix
  
Now go to this file

sudo nano /etc/postfix/main.cf

and change

myhostname = example.com

Put in name of your domain into myhostname.

If you want to have mail forwarded to other domains, replace alias_maps with virtual_alias_maps and point it to /etc/postfix/virtual.

virtual_alias_maps = hash:/etc/postfix/virtual

The rest of the lines are set by default. Save, exit, and reload the configuration file to put your changes into effect:

sudo /etc/init.d/postfix reload

Now run this for checking through terminal

sendmail sample-email@example.org

Friday, October 31, 2014

How to install PHPRoad (phpr) framework on php 5.5

Install PHPRoad on php 5.5:

PHPRoad is a framework which is used for developing web applications. But it has some limitations like it supports only php 5.4 and older version.

This Framework is not useful in case of php 5.5 . Because, some mysql extenstions does not support php 5.5 like mysql_connect. php 5.5 only supports mysqli_connect.

So, For installation of phpRoad in php 5.5 we have to follow the following steps

Step 1:

Firstly Download the zip and extract it on your local machine.

Step 2:

Run it on browser and follow the steps (Including creating key from phproad)

Here, you will face a problem during installation and that is about 
mysqli_connect

For removing it follow the Step 3

Step 3:
 Open this file

Your_application/framework/modules/db/classes/mysql_driver.php

(a). Here edit mysql_connect to mysqli_connect

also add $this->config['database'] in its Db::$connection (Because mysqli takes 4 parameters )

(b). Change all the mysql_ to mysqli_ in this file .

After this retry it on browser.

This time it will be installed successfully.

Thanks
Ashish Ginotra