#php

LEMP (Linux, Nginx, Mysql, PHP) in Ubuntu 14.04

In this post, I will show step to run a simple site in nginx server, use FPM.

Install all packet with apt-get:

Create example.com file in /etc/nginx/site-available/

Add the content below to “example.com” file

And enable the site you created

Now for the test

Install XHProf in Ubuntu 14.04

XHProf is a function-level hierarchical profiler for PHP and has a simple HTML based user interface. This article will show you the way to install and test XHProf in Ubuntu 14.04

a6f5639e-2cf3-4a71-91e4-250befcfd6a8-medium

When you talk, you are only repeating what you know; but when you listen, you learn something new

 

Install Graphviz:

Install XHProf from pecl:

Adding XHProf to PHP5:

Test:

Install XHProf report view:

Create a test script:

And view the report:

In the better way, you should create header.php, and footer.php and change apache2 configuration so that you can track your request automatically

 

Adding external library to Laravel 4+

When you work with a framework, sometimes you need to use an external library. This will show you the way to add an external library to Laravel 4. Actually, it is very simple.

For example, you have a library that locates in path: app/library/external_lib. So you should add that path to composer.json file (located at root directory of framework)

The next step is the final step, run command to dump library to autoload file of framework.

I assumed that composer was installed to your machine.

Laravel 4 and crontab

In many projects, you need use crontab (cron jobs) to execute some tasks as sending email or delete waste record in DB. With Laravel Project, you can do this easier.

Be strong enough to let go and patient enough to wait for what you deserve

Be strong enough to let go and patient enough to wait for what you deserve

Create a command in Laravel 4:

With command above, Laravel will create a file named FirstCommand.php in directory  app/commands/

FirstCommand.php contains source code as below:

You should change this line:

E.g:  I changed to

Laravel Command supports you use parameters in your command. To add or remove parameters, you should look at getArguments and getOptions functions

And the most important function is fire() function. The command will execute source code that is wrote in that function.

I did the most simple changed to show you the easiest to understand Laravel Command. Please see file FistCommand.php after changing:

Next step, you need to register the command with Laravel CLI. So easy, you open app/start/artisan.php file, and add one line as below:

You are done creating Laravel Command. To test, you could use command below:

The output above mean you successfully register a command.

Finally, put your command into the crontab:

Add line (run command every 2 minutes):

That’s all. Thank you for talking time to read this.

 

 

Laravel 4.2 Setup Working Environment

Laravel 4.2 setup working environment is an important task should do at the beginning a project. Config files, Git, Gitignore is things you should take care. The below will show you the way to store config files to separate working environment as development, staging and production.

A man who conquers himself is greater than one who conquers a thousand men in battle

A man who conquers himself is greater than one who conquers a thousand men in battle

Detect Working Environment boostrap/start.php:

For security reason, from Laravel 4.1,  detectEnvironment function only supports hostname.

Create directories in app/config:

Push config files (e.g database.php, app.php …) to directories that you pretend use for each environment, it will be override the default config value.

Laravel Multiple Database Connection

Laravel Multiple Database Connection – this is way to setup and work with multiple database connection in Laravel

The soul always knows what to do to heal itself. The challenge is to silence the mind

The soul always knows what to do to heal itself. The challenge is to silence the mind

Define connections in config file app/config/database.php

Use the second DB in Eloquent Model

Use the second DB Connection in Controller

Query Builder

 

 

Install Memcached, PHP Memcache and PHP Memcached in Ubuntu 14.04

As you know, Memcached is great cache system for your web application. Use correctly Memcached will speed up your application so much. So now is how to install memcached, PHP Memcache and PHP Memcached

Install Memcached, PHP Memcache and PHP Memcached in Ubuntu 14.04

In Ubuntu 14.04, the installation is very simple, just need one command:

Please remember restart apache2:

Finally, test your works by writing some PHP scripts.

Add php script to test.php:

Successful result:

 

 

Different between MyISAM and InnoDB

MyISAM means MYSQL Indexed Sequential Access Method. Basically, It is a flat file, and is very old technology. But do not let that put you off using it. Because a flat file means it is not relational, and thus is not an RDBMS, and thus is more appropriate for some situations.
InnoDB is the full RDBMS like almost one you know.  InnoDB is great for if you have a lot of relational links (with references, joins, etc).

With MyISAM, It implements locking on the table, and while that lock is held, no other session can perform a SELECT. With InnoDB, It implements locking on the row.

Additional thing, MyISAM supports full-text search.

Both MyISAM and InnoDB are good. But when you design database you need determine if you need features provided by InnoDB. If not, then MyISAM is up for consideration.

 

PHP create a class with dynamic variables

PHP provides overloading methods to help you create dynamically properties and methods. In this case we will use 2 functions __set() and get(). The below is the concept of  that two functions.

__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.

Now, we will write a class with dynamic variables using __get(), set() functions.

If you do not know about magic functions in PHP, this issue will is not easy for you. But, if you know it, it is so simple.