Friday, May 27, 2016

Comment a line after finding a string in file in php

Comment a line after finding a string in file in php:

I want to find a string in a file & comment the whole line in which that string is find out.

For this we have to use the following code:


            $path_to_root_file = "/path/of/file";
            $var = $this->getLineWithString($path_to_root_file, "find_string");
            $file_contents = file_get_contents($path_to_root_file);
            $file_contents = str_replace($var, "//$var", $file_contents);
            file_put_contents($path_to_root_file, $file_contents);

            function getLineWithString($fileName, $str) {
                $lines = file($fileName);
                foreach ($lines as $lineNumber => $line) {
                    if (strpos($line, $str) !== false) {
                        return $line;
                    }
                }
                return -1;
            }


Thanks

Friday, May 20, 2016

How to make helpers in Laravel 5

How to make helpers in Laravel 5:


 Many times we create some function that further used in different controllers. To avoid multiple creation of same function in multiple controller, we can write these common function in helpers.

 Custom helpers’ directory

Our helpers will be located in the /app directory

Create a new directory Helpers in /app/Helpers

Helper class definition

Let’s now create a simple helper function that will concatenate two strings. Create a new file MyFuncs.php in /app/Helpers/MyFuncs.php Add the following code

<?php

namespace App\Helpers;

class MyFuncs {

    public static function full_name($first_name,$last_name) {
        return $first_name . ', '. $last_name;
    }
}

HERE,

    1. namespace App\Helpers; defines the Helpers namespace under App namespace
    2. class MyFuncs {…} defines the helper class MyFuncs
    3. public static function full_name($first_name,$last_name) {…} defines a static function that accepts two string parameters and returns a concatenated string

Helpers service provide class

Service providers are used to auto load classes. We will need to define a service provider that will load all of our helper classes in /app/Helpers directory.

Run the following artisan command

php artisan make:provider HelperServiceProvider

The file will be created in /app/Providers/HelperServiceProvider.php

Open /app/Providers/HelperServiceProvider.php

Add the following code

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class HelperServiceProvider extends ServiceProvider {

   /**
    * Bootstrap the application services.
    *
    * @return void
    */
   public function boot()
   {
      //
   }

   /**
    * Register the application services.
    *
    * @return void
    */
   public function register()
   {
        foreach (glob(app_path().'/Helpers/*.php') as $filename){
            require_once($filename);
        }
   }
}HERE,

    1. namespace App\Providers; defines the namespace provider
    2. use Illuminate\Support\ServiceProvider; imports the ServiceProvider class namespace
    3. class HelperServiceProvider extends ServiceProvider {…} defines a class HelperServiceProvider that extends the ServiceProvider class
    4. public function boot(){…} bootstraps the application service
    5. public function register(){…} is the function that loads the helpers
    6. foreach (glob(app_path().'/Helpers/*.php') as $filename){…} loops through all the files in /app/Helpers directory and loads them.

Helper Service Provider and class alias configuration

We now need to register the HelperServiceProvider and create an alias for our helpers.

Open /config/app.php file

Locate the providers array variable

Add the following line

App\Providers\HelperServiceProvider::class,

Locate the aliases array variable

Add the following line

'MyFuncs' => App\Helpers\MyFuncs::class,

Save the changes
Using our custom helper

Add the following route definition

<?php

namespace App\Http\Controllers;

use App\Helpers\MyFuncs;

class CustomerController extends Controller
{

    public function getCustomerName($id)
    {
        $response = $this->getCustomerDetail($id);
        $msg = "Customer detail.";
        return MyFuncs::full_name($response->first_name, $response->last_name);
    }
}

Custom helpers can be used for commonly performed tasks. They are mostly helpful in views where you do not want to include business logic. You can create custom functions that format or process the data and return the results

Thanks.

Friday, May 6, 2016

Manually Authenticating Users In Laravel 5.1

Manually Authenticating Users In Laravel 5.1:

Of course, you are not required to use the authentication controllers included with Laravel. If you choose to remove these controllers, you will need to manage user authentication using the Laravel authentication classes directly.

We will access Laravel's authentication services via the Auth facade, so we'll need to make sure to import the Auth facade at the top of the class. Next, let's check out the attempt method:

<?php

namespace App\Http\Controllers;

use Auth;
use Illuminate\Routing\Controller;

class AuthController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}


The attempt method accepts an array of key / value pairs as its first argument. The values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array. If the two hashed passwords match an authenticated session will be started for the user.

The attempt method will return true if authentication was successful. Otherwise, false will be returned.

The intended method on the re director will redirect the user to the URL they were attempting to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.

If you wish, you also may add extra conditions to the authentication query in addition to the user's e-mail and password. For example, we may verify that user is marked as "active":

if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
    // The user is active, not suspended, and exists.
}

To log users out of your application, you may use the logout method on the Auth facade. This will clear the authentication information in the user's session:

Auth::logout();

Note: In these examples, email is not a required option, it is merely used as an example. You should use whatever column name corresponds to a "username" in your database.

Thanks.