Saturday, October 31, 2015

Route Model Binding using laravel 5.1

 What is Route Model Binding in laravel?

Ans: Laravel model binding provides a convenient way to inject class instances into  routes. For example, instead of injecting a user's ID, you can inject the entire User class instance that matches the given ID.

1. Open route.php and write following code .

Route::model('users', 'User');
Route::bind('users', function($value, $route) {
    return App\Task::whereSlug($value)->first();
)};
Route::resource('users', 'UsersController');

 Note: where User is Model name 

2. use users/{slug} as url instead of users/{id}

3. use controller method update({slug}) instead of update({id})

4. refrence site laravel.com

Saturday, October 24, 2015

Redis in Laravel

 Redis in Laravel:


Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, and sorted sets.

Configuration:

The Redis configuration for our application is stored in the app/config/database.php file. Within this file, we will see a redis array containing the Redis servers used by our application:

'redis' => array(

    'cluster' => true,

    'default' => array('host' => '127.0.0.1', 'port' => 6379),

),

Usage:

$redis = Redis::connection();
$redis = Redis::connection('other');
$redis->set('name', 'Taylor');

$name = $redis->get('name');

$values = $redis->lrange('names', 5, 10);
$values = $redis->command('lrange', array(5, 10));
Redis::set('name', 'Taylor');

$name = Redis::get('name');

$values = Redis::lrange('names', 5, 10);  

Friday, October 16, 2015

Creating a Top-Level Admin Page in Wordpress

The first step is to create a menu entry with the add_menu_page() function. Below is a full example:

add_action( 'admin_menu', 'my_admin_menu' );

function my_admin_menu() {
add_menu_page( 'My Top Level Menu Example', 'Top Level Menu', 'manage_options', 'myplugin/myplugin-admin-page.php', 'myplguin_admin_page', 'dashicons-tickets', 6  );
}

The function takes seven arguments. The first one is the page title, which defines the title tag; it is shown in the tab title, not on screen.

The second argument is the title that shows up in the menu.

Argument three is the capability required to access the menu. This can be used to restrict it to only admins, editors or authors.

Argument four is the menu slug, which is essentially used as the URL of the page.

Argument five is the function, which will handle the content of the page.

The next argument is the icon url. This can accept a number of formats. If a URL to an image is given the image will be used. We can also use Dashicons, which are built into WordPress, or even SVG.

The last argument defines where the menu will be placed. Argument five indicated the posts so I’ve used six to put this menu entry just underneath. Take a look at the Codex to see exactly what number to use for your desired position.

The next step is to create some content. All you need to do is create the function defined in argument five and echo some content. Here’s a very simple example we can start with:

function myplguin_admin_page(){
?>
<div class="wrap">
<h2>Welcome To My Plugin</h2>
</div>
<?php
}

Friday, October 9, 2015

How and why to use Middleware in Laravel 5.1

 Middleware in Laravel 5.1:


Middleware is used for check the request and then process it for particular url.

e.g. There are two types of users. One is Vendor and another is Users. Then for both kind of users will get different authorization.

For this:

Create a Middleware File

php artisan make:middleware VendorMiddleware

Now in routes.php we will set its url

route code
Route::group(['middleware' => 'auth'], function () {
    Route::get('/', ['middleware' => 'vendor', function () {
        return view('welcome');
    }]);


In VendorMiddleware:
<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class VendorMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user= Auth::user();
        if($user->user_type=='vendor'){
            echo "vendor login";
        }else{
            echo "you are unauthorized person";
        }
        //return $next($request);
    }
}


Write in app/Http/Kernel.php

 'vendor' => \App\Http\Middleware\VendorMiddleware::class,

Thanks