Pages

Monday 25 June 2018

Lumen Framework Installation (Laravel) for service

Here we are using lumen framework 5.0 (laravel) for service API and connecting with Drupal database.
Here we are using old version lumen 5.0. and install composer.

First command to run for install lumen-

composer create-project laravel/lumen assignment "5.0.*"

create .env file at root directory

write down this code for drupal database connection

APP_NAME=Lumen
APP_ENV=local
APP_DEBUG=true

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourdatabasename
DB_USERNAME=yourusername
DB_PASSWORD=youruserpasword

if this error comes then

NotFoundHttpException in Application.php line 1093:

change line in public/index.php file-

$app->run();

Into:

$app->run($app->request);

/*Lumen is not trying to load the “.env” file in your project.
For this, open up “bootstrap/app.php”,| and un-comment the following line:
*/

Dotenv::load(__DIR__.'/../');

/* PHP Fatal error: Class ‘Schema’ not found in
|  “Schema” is a facade, and once again, facade is disabled by default.
| We need to enable it, by un-commenting the following line in the “bootstrap/app.php”.
*/

 $app->withFacades();

 $app->withEloquent();

If some storage permission issue then give permission 777 /storage folder

Now check with this url its shows "lumen." at your system. localhost/projectname/public/

Now create a route in directory/app/Http/routes.php

$app->get('/hello', function () use ($app) {
    return 'Hello World';
});

hit on browser localhost/projectname/public/hello its shows
"Hello World " then its working now.

and write in routes.php

$app->get('/hellodata', 'App\Http\Controllers\helloController@getAllData');


Now creates a file helloController.php under this directory-

/var/www/html/projectname/app/Http/Controllers/

<?php
namespace App\Http\Controllers;
use DB;

use Laravel\Lumen\Routing\Controller as BaseController;

class helloController extends BaseController {

public function getAllData() {
$results = DB::select("SELECT * FROM tablename");
print_r($results);
     
   }
}








Thursday 10 August 2017

Laravel Basic Error

[:error] PHP Parse error:  syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 444



Run this command


sudo a2dismod php5 && sudo a2enmod php5.6 && sudo service apache2 restart

----------------------------------------------------------------------------------------------------------------------------------

[:error]SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

[:error]
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users`  add unique users_email_unique`(`email`))


Change in config/database.php


'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

Tuesday 4 July 2017

Contact Form submit more than 5 times in Drupal 8

Increase flood limit and flood interval in Drupal 8

Write down function in theme file

function theme_preprocess_page(&$variables) {
    $flood_limit = 500; //Default value is 5, change to whatever suits you.
        \Drupal::configFactory()->getEditable('contact.settings')
        ->set('flood.limit', $flood_limit)
        ->save();
}

There are two option below-:

//Default value is 5, change to whatever suits you. 

$flood_limit = 500;
        \Drupal::configFactory()->getEditable('contact.settings')
        ->set('flood.limit', $flood_limit)
        ->save();



//Default value is 3600sec (1hour), change to whatever suits you

$flood_interval = 3600;
 \Drupal::configFactory()->getEditable('contact.settings')
      ->set('flood.interval', $flood_interval)
      ->save();

Thursday 29 September 2016

Sending a mail with attachment in drupal

/* function for generate a CSV file  in drupal*/
 
function exportMobileCsv($filename, $column, $data) {
  $handle = fopen($filename, 'w+');
  fputcsv($handle, $column, ',');
  foreach ($data as $row) {
    fputcsv($handle, (Array)$row, ',');
  }
  file_save_data($handle, $filename);
  fclose($handle);
}


/* function for sending a mail in drupal*/

function mobileapp_mail_csv($from, $subject, $body, $filename) {
  $attachment = array(
    'filepath' => drupal_realpath($filename),
  );
  $mydate =  date('Y-m-d');
  $body = "<html><body>
    <p>
        Hi,
        <br/><br/><br/>Please find attached $mydate . <br />"."
    </p>
    <br/><br/>Regards,
    </body></html>";

  $to = 'hello@gmail.com';
  $headers['Cc'] = 'sumit.prajapati@rediff.com';
  $params = array(
    'to' => $to,
    'attachment' => $attachment,
    'body' => $body,
    'subject' => 'save',
    'headers' => $headers,
  );
  drupal_mail('mobileapp', 'mobileapp_mail_csv', $params['to'], language_default(), $params);
}



/* Function for attachemnt of file. */

function mobileapp_mail($key, &$message, $params) {
  switch ($key) {
    case 'mobileapp_mail_csv':
        $message['subject'] = $params['subject'];
        $message['body'][] = $params['body'];
      if (isset($params['attachment'])) {
        $message['params']['attachments'][] = $params['attachment'];
      }
      $message['headers'] += $params['headers'];
  }
}

Monday 26 September 2016

Display single column data break in multiple columns Mysql


Example Table -



nid sid cid data
938 6 1 Sumit
938 6 2 sau@gmail.com
938 6 3 9878990677
938 7 1 Priya
938 7 2 priya@gmail .com
938 7 3 9878990677
938 8 1 kishan
938 8 2 kishan@gmail.com
938 8 3 9878990677
938 9 1 Sunil
938 9 2 sunil@gmail.com
938 9 3 9878990677
938 10 1 Vika
938 10 2 vik@gmail.com
938 10 3 9878990677
938 11 1 Jain
938 11 2 sab@gmail.com
938 11 3 9878990677
938 12 1 Johari
938 12 2 Johari@gmail.com
938 12 3 9878990677

Now we want to


 sid Name Email Mobile
6 Sumit sau@gmail.com 9878990677
7 Priya priya@gmail .com 9878990677
8 kishan kishan@gmail.com 9878990677
9 Sunil sunil@gmail.com 9878990677
10 Vika vik@gmail.com 9878990677
11 Jain sab@gmail.com 9878990677
12 Johari Johari@gmail.com 9878990677

SELECT wsd.sid,
MAX( IF( wsd.cid =1, wsd.data, NULL ) ) AS `Name` ,
MAX( IF( wsd.cid =2, wsd.data, NULL ) ) AS `Email` ,
MAX( IF( wsd.cid =3, wsd.data, NULL ) ) AS `Mobile` FROM webform_submitted_data wsd WHERE nid =938 Group By sid

Explanation- 

IF(condition, value1, value2) 

if condition = value met than its print single value in row other two value will be null
Null value is zero
So we use max for highest value its print all value in single row 


Tuesday 3 May 2016

Unzip/extract a .tar.gz file?

Unzip/extract a .tar.gz file?

tar -xvzf community_images.tar.gz

sql.gz file to my database? (importing)

gunzip < myfile.sql.gz | mysql -u root -p mydb


ubuntu command to move a directory

mv -if html/backup /var/www/hep


Copy Content Local to Remote Server Command

rdesktop -r clipboard:CLIPBOARD -f -u'username' -p'password' IP

Wednesday 9 March 2016

Create custom Token for Password

/**
 * Implements hook_token_info().
 */
function module_name_token_info() {
  $info['tokens']['user']['password'] = array(
    'name' => t('Password'),
    'description' => t('The password by the user'),
  );
  return $info;
}

/**
 * Implements hook_tokens().
 */
function module_name_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  $url_options = array('absolute' => TRUE);
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
  }

  if ($type == 'user' && !empty($data['user'])) {
    $account = $data['user'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'password':
          $replacements[$original] = $account->password;
          break;
      }
    }
  }

  return $replacements;
}