Undoubtedly logging is most important part of an application to debugging, analyzing, measuring performance. So often times we want to handle different logging behavior for different components of our application. Assume that you are developing an e-commerce website and you want to handle logging cart & payment component differently.
So in that way, each log of the different component will store into a different file. So debugging will be easier than generic logging. To do that open config/bootstrap.php file and copy pastes this snippet.
Then you can implement this scope like this way (From controller & model).
Log::error('Product is not added into cart due to internal error', ['scope' => ['cart']]);
Log::emergency('Payment is not received', ['scope' => ['payments']]);
Sometimes programmer needs to chunk or grouping array based on the array key value. Look at the below code snippet, here I have a multidimensional array, and I want to group all same elements of each category.
If you see the error notice properly you can understand what does it means. This error notice means some of your PHP has old constructors. So if you replace these old constructors with new constructor then the problem will be solved.
To do this, open streems.php file by this command.
sudo gedit /usr/share/php/php-gettext/streams.php
StringReader Error: To find it go to line number 52 then
//Replace this line
function StringReader ($str='') {
//with
function __construct($str='') {
FileReader Error: To find it go to line number 90 then
//Change this
function FileReader ($str='') {
//To
function __construct($str='') {
CacheFileReader Error: To find it go to line number 146 then
//Change this
function CacheFileReader ($str='') {
//To
function __construct($str='') {
Then save & close the file. After that open gettext.php file by this command.
sudo gedit /usr/share/php/php-gettext/gettext.php
Go to line number 101 then
// Replace this line
function gettext_reader($Reader, $enable_cache = true) {
//With
function __construct($Reader, $enable_cache = true) {
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.phpandHomeController.php. Now you want to access:
index() action from all controller
lists() action of ClientsController.php controller
jobs() action of JobsController.phpcontroller
home() action of HomeController.phpcontroller
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.
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.
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)
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.
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.
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
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.
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:
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.