Categories
Career PHP Tips and Tricks Tools and Technologies

How to Recursively Delete a Directory and its Entire Contents Using PHP

Here is a script for recursively delete a directory and its entire contents using PHP

Categories
CakePHP Career Code Area PHP Programming Tips and Tricks Tools and Technologies

CakePHP Auth Allow to Specific Actions of Specific Controllers

It’s a common problem to CakePHP developer to auth allow to specific actions of a specific controller. Especially to newbies developer of CakePHP. Honestly, it’s really easy but little tricky. So this article is for whom, who looking for CakePHP auth allow to specific actions of specific controllers.

Assume you have three controllers ClientsController.php, JobsController.php and HomeController.php. Now you want to access:

  • index() action from all controller
  • lists() action of  ClientsController.php controller
  • jobs() action of JobsController.php controller
  • home() action of HomeController.php controller

To accomplish this goal open AuthController.php file and add these following line.

public function beforeFilter(Event $event)
{
   $this->Auth->allow(array('index'));
}

 

Then open ClientsController.php file and add these following line.

public function beforeFilter(Event $event)
{
   parent::beforeFilter($event);
   $this->Auth->allow(array('lists'));
}

 

Then open JobsController.php file and add these following line.

public function beforeFilter(Event $event)
{
   parent::beforeFilter($event);
   $this->Auth->allow(array('jobs'));
}

 

Then open HomeController.php file and add these following line.

public function beforeFilter(Event $event)
{
   parent::beforeFilter($event);
   $this->Auth->allow(array('home'));
}

In this way, you can auth allow to specific actions of specific controllers.

Categories
Career Code Area PHP Tips and Tricks Tools and Technologies

Catch PHP Output Buffering Data Over jQuery AJAX

From last few day’s I was deeply engaged with Output Buffering. I had to catch PHP output buffering responsive over AJAX request. And now I am here to sharing a sample script to catch PHP output buffering data over jQuery AJAX.

Live Demo

 

Categories
Career Code Area PHP Tips and Tricks Tools and Technologies

Upload File Using cPanel API

cPanel is a web hosting control panel which is using for various aspects of website and server administration with a nice graphical interface. cPanel also has well-documented APIs to manage all kinds cPanel activities through API. Here I have shared a code snipped to uploading files using cPanel API. (Fileman::upload_files)

Categories
Career Code Area PHP Tips and Tricks Tools and Technologies Uncategorized

Parsing XML With Multiple Namespaces Using PHP

Yesterday, I had a very ugly XML data which had to parse. It has multiple namespaces and namespaces are irrelevant with DOM – I just wasn’t getting the nodeValue from the Element. It was parsing partially not all nodeValues due to namespaces are irrelevant with DOM. Then I applied little tricks onto it through XML children options. And I thought I need to share this code so other developers can save his time to get out this annoying issue.

Categories
Career Code Area Tips and Tricks Tools and Technologies

Remove Default Search Box and Add Custom Box in DataTable

DataTable displaying a default search box while initializing it. So by default DataTable has a search box. But sometimes you might have to remove that default search box and add a custom search box. And place that somewhere in the page. This article will help you to do that.

$('#example').DataTable();

See the above snippet, If your DataTable initializing it like that, then default search box will display. You might think, I can just use “bFilter: false” option to remove that search box. Yes you can! but the problem is if you do that, then DataTable will disable/turn off the search functionality. But you have to only remove the search box not search functionality.

In this case, you can use "dom: 't'" option. This will keep the default functionality of DataTable and just remove the search box, pagination (just hide it). It will just show the table elements.

Here I have shared a code snippet, you can get the idea to accomplish it.

$(document).ready(function() {
  var users = $('#users').DataTable({
    "dom": "t"
  });

  $('#customSearchBox').keyup(function() {
    users.search($(this).val()).draw();
  })
});

See the full jsFiddle

Categories
Career Code Area Tips and Tricks Tools and Technologies

Install Elasticsearch in Ubuntu 16.04

Elasticsearch is a distributed, real-time, full-text search engine. Elasticsearch developer by JAVA (based on Lucene). It has an HTTP web interface and schema-free JSON documents. In this article, I will describe how to install and then configure elasticsearch in ubuntu 16.04. Let’s see

Before you install elasticsearch, please ensure java is installed on your machine. If not then install java first before doing anything.

Install Elasticsearch in Ubuntu 16.04
Install Elasticsearch in Ubuntu 16.04

Installing Java 8

 First Add the Java PPA to apt package database.

sudo add-apt-repository -y ppa:webupd8team/java

Then run update commands to update your apt package database.

sudo apt-get update
Then install Java 8 and verify it is installed, run following command.

sudo apt-get -y install oracle-java8-installer
java -version
After successfully install Java, now you can install  Elasticsearch on your machine.

Installing and Configuring Elasticsearch

You can download elasticsearch from their official website elastic.co. It would be always good to download deb (Debian) version for the ubuntu, cause it has everything integrated to successfully run elasticsearch. You can also download it through the command line:

sudo wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.deb
Then install this as usual ubuntu way with the dpkg command:

sudo dpkg -i elasticsearch-1.7.2.deb

After execution this command your installation will be completed, now you have to configure elasticsearch. By default all configuration files of elasticsearch stored into the /etc/elasticsearch directory. Here you can get two files elasticsearch.yml and logging.yml. To configure the elasticsearch you have to open elasticsearch.yml file in your text editor.

sudo nano /etc/elasticsearch/elasticsearch.yml

Now find the line node.name and cluster.name in this file and uncomment it (remove # before them) and change the value of these two.

Find the line, network.bind_host uncomment it by removing the # character at the beginning of the line, and change the value to 0.0.0.0 so it looks like this:

network.bind_host: 0.0.0.0

After all done, elasticsearch will be installed on your computer, now just run this command to start elasticsearch.

sudo service elasticsearch start

For testing elasticsearch output. just type http://localhost:9200/ in your browser and see the possible response.

{
  "status" : 200,
  "name" : "sohel-rana-pc",
  "cluster_name" : "sohel-rana-elasticsearch",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}
Categories
CakePHP Career Code Area PHP Tips and Tricks Tools and Technologies

Internationalization & Localization Application with CakePHP 3

CakePHP has one of the coolest features to make application Internationalization & Localization. It’s really easy to translate your application or website into multiple languages with CakePHP 3.  Let’s see it.

First of all, create a directory Locale inside PROJECT_ROOT/src

Create a subdirectory of each language (which language you want to translate your application) inside PROJECT_ROOT/src/Locale/. Subdirectory name can be anything (ex. english, french, etc), but prefer ISO code of the language (ex. en_US, fr_FR, bn_BN etc). So follow this tutorial and make your subdirectory name en_USfr_FR, and bn_BN

Create default.po file inside all subdirectories (en_USfr_FR, and bn_BN). And copy the following code in the respective file.

src/Locale/en_US/default.po

msgid "message"
msgstr "Hello World"

 

src/Locale/fr_FR/default.po

msgid "message"
msgstr "Bonjour le monde"

 

src/Locale/bn_BN/default.po

msgid "message"
msgstr "ওহে বিশ্ব"

Here msgid key of which will use inside the template (view files) and msgstr is the values which will gonna translate.

So now you have three different languages (en_USfr_FR, and bn_BN). and one key (message) which can be translated into these three languages.

Now use <?php echo __(‘message’); ?> in your view (any ctp file of your application).

Then open PROJECT_ROOT/config/app.php file and find

 'defaultLocale' => env('APP_DEFAULT_LOCALE', 'fr_FR')

and replace with your language.

But if you want to translate language in runtime then you can use it

use Cake\I18n\I18n;

I18n::locale('fr_FR');

I hope this article will help you to translate your application in multiple language.

Categories
Career Code Area Google Cloud Tips and Tricks

Host Static Website in Google App Engine

This article is for hosting your static website into Google App Engine. This article provides an app.yaml file so you can host your static website into Google App Engine. Before do anything make your website’s folder structure like that (see below image). So put all the static files inside assets folder (css, js, images, fonts) and all HTML files in project’s root directory. Inside assets folder css, js, img, and fonts are sperate folders for holding separate files (ex. css file stylesheets, img for images)

Host Static Website in Google App Engine

Then copy-paste this snipped in your YAML file. And then deploy (gcloud app deploy) your application.

You can also make your project structure your own way, then you have to change YAML content based on your changes. So that means need to change YAML information according to your project’s structure.

Categories
Career Nginx Ubuntu

Create NGINX Virtual Host with PHP 7.0

Here I have shared a script for creating NGINX virtual host with PHP 7.0. NGINX virtual host with  5.x and 7.0 is very similar but only different is php5-fpm.sock location. To create a NGINX virtual host open the configuration files by running this command.

sudo gedit /etc/nginx/sites-available/default

And then copy-pastes this code snippet and modify it by your configuration (information). Then restart your server. And you are ready to go.

See it for creating virtual host with PHP 5.x