Laravel Grid with Datatables

In this series will be looking at configuring datatables with Laravel.

Such things are useful when you are working with some admin panel or CRUD operations.

Before you start, you should have basic structure knowledge of laravel and working setup and composer installed and accessible from command line along with php.

If you dont have then following commands to setup your project

  1. Install => composer create-project --prefer-dist laravel/laravel admincms
  2. setup your preferred database credentials in .env file
  3. Generate application key =>php artisan key:generate (this step may or may not required.)
  4. Run migrations to populate the desired tables => in this case users table => php artisan migrate

Once all  this done then try your installation by running local server by php artisan serve

In this step if got error for port already occupied or cannot start development server then try another port with –port:8080

You can see default laravel page in browser, that confirms your setup is working is fine

Here we are using DATATABLES and Composer Package

Install package via composer

composer require yajra/laravel-datatables-oracle

Once everything is fine then you will see following

composer-datatables

Define Providers and Aliases in /config/app.php

'providers' => [
     //...
   yajra\Datatables\DatatablesServiceProvider::class,
],

'aliases' => [
     //....
     'Datatables' => yajra\Datatables\Datatables::class,
],

 

Now to check whether datatables working fine we need to have enough data for pagination in db. If not available  then generate some random data just for testing purpose

php artisan tinker

factory(App\User::class, 100)->create();
generate data.png
you should see something like above screen which will populate data in db. cross check with db
Please note that you should take care of your existing data first before running this.
Next step is create controller and routes
php artisan make:controller DisplayDataController --resource
It will create DisplayDataController.php file
Configure routes as follows in /routes/web.php
Route::get('create', 'DisplayDataController@create');
Route::get('index', 'DisplayDataController@index');
In DisplayDataController.php make following changes

1)

use App\User;

use Yajra\DataTables\Facades\DataTables;
2) Modify Index function as
public function index()
{
     return DataTables::of(User::query())->make(true);
}
Modify create function as
public function create()
{
    return view('displaydata');
}
Add required datatables js and css
Add table structure in template as follows
<table class="table table-bordered" id="table">
    <thead>
       <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Email</th>
       </tr>
   </thead>
</table>
Add javascript function to initialize the datatables
         $(function() {
               $('#table').DataTable({
               processing: true,
               serverSide: true,
               ajax: '{{ url('index') }}',
               columns: [
                        { data: 'id', name: 'id' },
                        { data: 'name', name: 'name' },
                        { data: 'email', name: 'email' }
                     ]
            });
         });
And thats it, you are done
Visit the newly created route in browser in this case “create”
You should see something like
grid.png
Feel free to play around with columns and other datatables functionality.
Advertisements

Swagger UI for PHP based Applications

First of all you need to understand what swagger is.. In simple words Swagger is the world’s largest framework of API developer tools for the OpenAPI Specification(OAS).

In another words Swagger is a annotations library for generating Swagger compatible JSON documentation for your API.

The resulting JSON documentation may then be utilized for internal and external user friendly documentation, API portal sandbox with Swagger UI.

Its not needed that you need to follow specific php framework to integrate it, but your existing code, may be 8-10 years old or recent once, you can integrate it without any hassle.

Prerequisite:

  1. PHP should be installed on your system,
  2. PHP command line should work On Linux system its awesome, on windows system try adding a php executable path in environment variables. check with php -v to get php version and confirm that command line is working fine.
  3. composer should work on your command line. (on windows if first time then you are surely face some weird issues. get that sorted first)

thats it you are ready to go now..

Now further you need to follow following some steps to integrate worlds leading annotations library into your project

  1. Download Swagger.phar
  2. Add swagger annotations to your php file or php classes
  3. Generate swagger JSON file
  4. Install swagger UI Project (mainly to view client to check API document )
  5. Open swagger UI and feel proud.

Important note: You can have swagger annotations for your one project at a time, for multiple projects you need to configure it separately.

Now will explain each of the steps in detail.

Please note that the commands referenced here were used and worked fine on Ubuntu systems, for windows users are requested to make appropriate changes to get that work.

Download Swagger.phar

First and most important step is to download swagger.phar file via composer. It will be easy for you if you create a separate folder for swagger related tasks, create a “swaggergenerator” folder.

Execute following command on command line to get it .

cd SwaggerGenerator
 php composer require zircote/swagger-php

 

After successful completion of above commands you should have swagger file and other related files in your directory

copy swagger file from swaggergenerator/vendor/zircote/swagger-php/ to  swaggergenerator folder

 

Add Swagger Annotations to existing php file

/**
* @SWG\Post(path="/user/login",
* tags={"user"},
* summary="Logs user into the system",
* description="User login to system",
* operationId="loginUser",
* produces={"application/xml", "application/json"},
* @SWG\Parameter(
* name="username",
* in="header",
* description="The user name for login",
* required=true,
* type="string"
* ),
* @SWG\Parameter(
* name="password",
* in="header",
* description="The password for login in clear text",
* required=true,
* type="string"
* ),
* @SWG\Response(
* response=200,
* description="successful operation",
* @SWG\Schema(type="string"),
* @SWG\Header(
* header="X-Rate-Limit",
* type="integer",
* format="int32",
* description="calls per hour allowed by the user"
* ),
* @SWG\Header(
* header="X-Expires-After",
* type="string",
* format="date-time",
* description="date in UTC when token expires"
* )
* ),
* @SWG\Response(response=400, description="Invalid username/password supplied")
* )
*/

Here in short you are defining an api call with input and output parameter, response headers and specific response formats if any.

Add documentation comments in all possible locations to annotate your code, you can refer to Open API specifications format mentioned above, complete the annotations tasks and move ahead

 

Generate Swagger JSON file

Create a separate documentation folder called swaggerdocs or any other of your choice

now navigate to swaggergenerator folder created in previous steps and execute following command

cd swaggergenerator
php swagger /var/www/html/my/project/path -o /var/www/html/my/project/path/swaggerdocs
in short php swagger source -o destination

Verify that json file got created in the destination folder, this is the important file which used to show the swagger UI in browser.

Install Swagger UI project

Download swagger ui project from HERE  and place it in swaggerui folder (create a new folder in your project directory)

Edit ui/index.html and change the default  URL in javascript so that it picks up your domain.

url = window.location.protocol + "//" + window.location.hostname + "/swagger/docs/"

Open swagger UI in browser http://your-project-path/swaggerui/

If you done all above things correctly you will get below screen

Note that we have added documentation for only single API or function, As you go on adding for different one, you will get that listed in swagger UI

Note that you need to generate json file once again when you finished adding annotations in your files.

If you want to avoid that then use a build tool and add the generate command in build process.

Drop a comment if have any query or suggestions.

Enjoy…..

PHP functional HTML rendering

Working with HTML integration while writing code is sometimes tedious and time consuming, most of the times just copy paste.

Recently viewed a post about adding some fun in doing so.

Some may found it complicated, some may found unnecessary. but someone who are keen to explore new things or hidden things here is example.

 

First start with two basic functions that will carry out most of our complicated things

 

function tag(){
$args = func_get_args();
$nm = array_shift($args);
$content = array_shift($args);
$attrs = array_chunk($args, 2);
echo “<$nm “;
foreach($attrs as $chunk)
echo $chunk[0].’=’.'”‘.$chunk[1].'” ‘;
echo ‘>’;
if(is_string($content) || is_numeric($content) || empty($content))
echo $content;
else
$content();
echo “</$nm>”;
}

function etag(){
$args = func_get_args();
$nm = array_shift($args);
$attrs = array_chunk($args, 2);
echo “<$nm “;
foreach($attrs as $chunk)
echo $chunk[0].’=’.'”‘.$chunk[1].'” ‘;
echo “/>”;
}

So the first argument to our tag function is the tag name, second is the content to render inside the tag and the rest of the arguments are the attribute name and value pairs. Same goes for the etag function which renders empty elements, but then sans the content argument of course.

Below are a few examples of usage, first a simple login form.

tag(‘div’, function(){
tag(‘form’, function(){
echo ‘Username:’;
etag(‘input’, ‘type’, ‘text’, ‘name’, ‘username’);
echo ‘Password:’;
etag(‘input’, ‘type’, ‘password’, ‘name’, ‘password’);
etag(‘input’, ‘type’, ‘submit’, ‘value’, ‘Submit’, ‘name’, ‘submit’);
}, ‘method’, ‘post’);
}, ‘class’, ‘normal-pad’);

Then a more complicated scenario where we want to render a table of statistics:

function statsTable($stats, $headers, $fields, $sums){
tag(‘table’, function() use ($stats, $sums, $headers, $fields){
tag(‘tr’, function() use ($headers){
foreach($headers as $header)
tag(‘th’, $header);
});
foreach($stats as $s){
tag(‘tr’, function() use ($s, $fields){
foreach($fields as $field)
tag(‘td’, $s[$field]);
});
}
tag(‘tr’, function() use ($sums, $fields){
foreach($fields as $field)
tag(‘td’, is_numeric($sums[$field]) ? $sums[$field] : ”);
}, ‘class’, ‘sum-row’);
}, ‘class’, ‘stats-table’);
}

 

 

Nutch 2.3 + ElasticSearch 1.4 + HBase 0.94 Setup

Info

This guide sets up a non-clustered Nutch crawler, which stores its data via HBase. We will not learn how to setup Hadoop et al., but just the bare minimum to crawl and index websites on a single machine.

Terms

  • Nutch – the crawler (fetches and parses websites)
  • HBase – filesystem storage for Nutch (Hadoop component, basically)
  • Gora – filesystem abstraction, used by Nutch (HBase is one of the possible implementations)
  • ElasticSearch – index/search engine, searching on data created by Nutch (does not use HBase, but its down data structure and storage)

Requirements

Install OpenJDK, ant and ElasticSearch via your repository manager of choice (ES can be installed by using the .deb linked above, if you need).

Extract Nutch and HBase somewhere. From now on, we will refer to the Nutch root directory by$NUTCH_ROOT and the HBase root by $HBASE_ROOT.

Setting up HBase

  1. edit $HBASE_ROOT/conf/hbase-site.xml and add
    <configuration>
      <property>
        <name>hbase.rootdirname>
        <value>file:///full/path/to/where/the/data/should/be/storedvalue>
      property>
      <property>
        <name>hbase.cluster.distributedname>
        <value>falsevalue>
      property>
    configuration>
  2. edit $HBASE_ROOT/conf/hbase-env.sh and enable JAVA_HOME and set it to the proper path:
    -# export JAVA_HOME=/usr/java/jdk1.6.0/
    +export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/

    This step might seem redundant, but even with JAVA_HOME being set in my shell, HBase just didn’t recognize it.

  3. kick off HBase:
    $HBASE_ROOT/bin/start-hbase.sh

Setting up Nutch

  1. enable the HBase dependency in $NUTCH_ROOT/ivy/ivy.xml by uncommenting the line
    <dependency org="org.apache.gora" name="gora-hbase" rev="0.5" conf="*->default" />
  2. configure the HBase adapter by editing the $NUTCH_ROOT/conf/gora.properties:
    -#gora.datastore.default=org.apache.gora.mock.store.MockDataStore
    +gora.datastore.default=org.apache.gora.hbase.store.HBaseStore
  3. build Nutch
    $ cd $NUTCH_ROOT
    $ ant clean
    $ ant runtime

    This can take a while and creates $NUTCH_ROOT/runtime/local.

  4. configure Nutch by editing $NUTCH_ROOT/runtime/local/conf/nutch-site.xml:
    <configuration>
      <property>
        <name>http.agent.namename>
        <value>mycrawlernamevalue> 
      property>
      <property>
        <name>http.robots.agentsname>
        <value>mycrawlernamevalue> 
      property>
      <property>
        <name>storage.data.store.classname>
        <value>org.apache.gora.hbase.store.HBaseStorevalue>
      property>
      <property>
        <name>plugin.includesname>
        
        <value>protocol-httpclient|urlfilter-regex|parse-(text|tika|js)|index-(basic|anchor)|query-(basic|site|url)|response-(json|xml)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)|indexer-elasticvalue>
      property>
      <property>
        <name>db.ignore.external.linksname>
        <value>truevalue> 
      property>
      <property>
        <name>elastic.hostname>
        <value>localhostvalue> 
      property>
    configuration>
  5. configure HBase integration by editing $NUTCH_ROOT/runtime/local/conf/hbase-site.xml:
    <configuration>
      <property>
        <name>hbase.rootdirname>
        <value>file:///full/path/to/where/the/data/should/be/storedvalue> 
      property>
      <property>
        <name>hbase.cluster.distributedname>
        <value>falsevalue>
      property>
    configuration>

That’s it. Everything is now setup to crawl websites.

Adding new Domains to crawl with Nutch

  1. create an empty directory. Add a textfile containing a list of seed URLs.
    $ mkdir seed
    $ echo "https://www.website.com" >> seed/urls.txt
    $ echo "https://www.another.com" >> seed/urls.txt
    $ echo "https://www.example.com" >> seed/urls.txt
  2. inject them into Nutch by giving a file URL (!)
    $ $NUTCH_ROOT/runtime/local/bin/nutch inject file:///path/to/seed/

Actual Crawling Procedure

  1. Generate a new set of URLs to fetch. This is is based on both the injected URLs as well as outdated URLs in the Nutch crawl db.
    $ $NUTCH_ROOT/runtime/local/bin/nutch generate -topN 10

    The above command will create job batches for 10 URLs.

  2. Fetch the URLs. We are not clustering, so we can simply fetch all batches:
    $ $NUTCH_ROOT/runtime/local/bin/nutch fetch -all
  3. Now we parse all fetched pages:
    $ $NUTCH_ROOT/runtime/local/bin/nutch parse -all
  4. Last step: Update Nutch’s internal database:
    $ $NUTCH_ROOT/runtime/local/bin/nutch updatedb -all

On the first run, this will only crawl the injected URLs. The procedure above is supposed to be repeated regulargy to keep the index up to date.

Putting Documents into ElasticSearch

Easy peasy:

$ $NUTCH_ROOT/runtime/local/bin/nutch index -all

Query for Documents

The usual ElasticSearch way:

$ curl -X GET "http://localhost:9200/_search?query=my%20term"

How to install memcache on windows

Check your operating system whether 32bit or 64 bit. Based on that you need to download the binary version.

  1. 64bit Os – http://s3.amazonaws.com/downloads.northscale.com/memcached-win64-1.4.4-14.zip

  2. 32bit Os – http://code.jellycan.com/memcached/

  • Place the binary file on C or D location
  • Now Run the Command Prompt as Administrator – type the cmd –  C:/memcached/memcached.exe  -d install
  • Once Installed, Start the service – C:\memcached\memcached.exe -d start
  • Verify the service running properly on Start- > Run -> services.msc
  • Check your php extensions directory for php_memcache.dll
  • If you don’t have it, Download http://pecl.php.net/package/memcache/3.0.8/windows
  • Now Edit the Php.ini file and add following lines at extension section
  1. extension=php_memcache.dll
  2. Restart your apache server and Now we are good go.

If you are using Drupal then add below lines to settings.php

$conf['cache_backends'][] = 'sites/all/modules/contrib/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';

you can check the memcahce settings at “admin/reports/memcache”

 

If you are using Core php then use below code for checking memcache working or not.

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>

Install SASS on Windows

  • The fastest way to get Ruby on your Windows computer is to use Ruby Installer.
  • After Ruby install Go to command prompt, C:\Ruby200-x64\bin folder use following command to install the SASS.
  • gem install sass
  • check version of SASS using following command
  • sass –v
  • install compass using following command
  • gem install compass
  • After install compass add ruby bin path i.e. C:\Ruby200-x64\bin in your local system environment variables path (My computer => properties => Advanced system settings => Advanced => Enviroment variables => path )
  • Restart the system.
  • Using Command prompt go to your project folder i.e. C:\xampp\htdocs\example
  • Compass watch
  • Change in css file and see the information in command prompt

Using Twig library with Codeigniter

Quite few days back worked on TWIG the template engine for PHP and tried if can be used in my existing codeigniter setup

Want to share following quick steps which may be useful for using with codeigniter

Step 1

Create Twig cache directory under “application / cache ” folder and make sure its writable..

Step 2

Download the TWIG library HERE and put in libraries folder so it will follow following directory structure

application/libraries/Twig
|-- Error
|-- Extension
|-- Filter
|-- Function
|-- Loader
|-- Node
|   `-- Expression
|       |-- Binary
|       `-- Unary
|-- NodeVisitor
|-- Sandbox
|-- Test
`-- TokenParser

Now create Library files

File 1=> application/libraries/Twig.php

################################################

<?php if (!defined(‘BASEPATH’)) {exit(‘No direct script access allowed’);}
class Twig
{
private $CI;
private $_twig;
private $_template_dir;
private $_cache_dir;
/**
* Constructor
*
*/
function __construct($debug = false)
{
$this->CI =& get_instance();
$this->CI->config->load(‘twig’);
ini_set(‘include_path’,
ini_get(‘include_path’) . PATH_SEPARATOR . APPPATH . ‘libraries/Twig’);
require_once (string) “Autoloader” . EXT;
log_message(‘debug’, “Twig Autoloader Loaded”);
Twig_Autoloader::register();
$this->_template_dir = $this->CI->config->item(‘template_dir’);
$this->_cache_dir = $this->CI->config->item(‘cache_dir’);
$loader = new Twig_Loader_Filesystem($this->_template_dir);
$this->_twig = new Twig_Environment($loader, array(
                ‘cache’ => $this->_cache_dir,
                ‘debug’ => $debug,
));
foreach(get_defined_functions() as $functions) {
             foreach($functions as $function) {
                 $this->_twig->addFunction($function, new Twig_Function_Function($function));
             }
         }
}
public function add_function($name)
{
$this->_twig->addFunction($name, new Twig_Function_Function($name));
}
public function render($template, $data = array())
{
$template = $this->_twig->loadTemplate($template);
return $template->render($data);
}
public function display($template, $data = array())
{
$template = $this->_twig->loadTemplate($template);
/* elapsed_time and memory_usage */
$data[‘elapsed_time’] = $this->CI->benchmark->elapsed_time(‘total_execution_time_start’, ‘total_execution_time_end’);
$memory = (!function_exists(‘memory_get_usage’)) ? ‘0’ : round(memory_get_usage()/1024/1024, 2) . ‘MB’;
$data[‘memory_usage’] = $memory;
$template->display($data);
}
}
###########################################

File 2 application/config/twig.php

##############################################
<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
$config[‘template_dir’] = APPPATH.’views’;
$config[‘cache_dir’] = APPPATH.’cache/twig’;
###################################3

Step 3 USAGE

Put in you controller
$this->load->library('twig');

$data['title'] = "twig loaded";

$this->twig->display('view.html', $data);


simple huh…..

 

reference