#PHP

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

 

How to integrate phpCAS in Laravel

I would like to share you how to integrate phpCAS in Laravel. As you know, Jasig CAS is a Single-Sign-On System. If you want to connect to Jasig CAS from your system (that is wrote by PHP), you use phpCAS Library

Work hard in Silence, Let Success make the noise

Work hard in Silence, Let Success make the noise

To know how to install a Jasig CAS server, please see:  https://sonnguyen.ws/install-jasig-cas-ubuntu-14-04/

Step 1:  Download phpCAS Library and locate it in app/library directory

Step 2: Create CAS Authentication Provider

We create file CasAuthProvider.php

Add source code to file:

Please make note that, you have to add  cas_host, cas_context, cas_port to app/config/app.php file

Step 3: Adding new provider to Laravel

Edit app/start/global.php file by adding code:

Step 4: Adding phpCAS library, new provider to Laravel Autoload

Open composer.json located at Laravel Root Directory and add app/library/phpCAS, app/cas:

Dump new Laravel Autoload file:

Step 5: Finally, create route and controller for login action

In app/routes.php file add:

In app/controllers/Admin.php add:

 

 

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.

MYSQL Command should remember

This is just to save some mysql command that sometimes I forget. The store is good for me when I need to look back and copy paste.

1. Faster import sql file

Export and Compress sql file:

Decompress and Import:

Create new user and pass:

Grant privileges:

List of the “in progress” process:

 

Apache Thrift Hello World with PHP and Python Part 2

In the previous post – Apache Thrift Hello World with PHP and Python Part 1, I wrote how to code server python with Apache Thrift. And, the second part, and it is the last one as well, I will show you the way how to writing the client with Laravel 4.2

You could clone full source from here: https://github.com/thanhson1085/thrift-laravel

Please download the source code before reading next steps.

1feac2f8-2d86-11e4-b8e7-12313d026081-medium

What you get by achieving your goals is not nearly as important as what you become by achieving your goals

Step 1: Add Apache Thrift client libraries to Laravel

Create directory: app/libs

Add put all external libraries to that directory.

Includes libraries to Laravel autoload by adding the lib path to composer.json file:

And run composer update :

Step 2: Add a route

Add routes (app/routes.php):

Step 3: Create Hello Controller

Laravel HelloController (HelloController.php):

With source code above, You can see that thrift client will be connect to server at port 9093, localhost, and timeout is 3600 seconds.

Step 4: Run a Test

Run Thrift Server (refer to part 1):

Run client request:

Apache Thrift Hello World with PHP and Python Part 1

In the previous post, I shared you the way how to install Apache Thrift in Ubuntu server. And now, I am writing the first application named Hello World with Apache Thrift in Python and Laravel 4.2

Concept:

You could clone full source from here: https://github.com/thanhson1085/thrift-laravel

Please download the source code before reading next steps.

You never know how strong you are, until being strong is your only choice

You never know how strong you are, until being strong is your only choice

Step 1: Create thrift files hello.thirft and exceptions.thrift

hello.thrift:

exceptions.thrift:

Step 2:  Run thrift compiler command to generate source code

See the details at gen.sh file

Step 3: Code the server-side

Code Hello Server hello-server.py (located src/python directory):

Create Hander hander.py located in src/python/hello:

Step 4: Run a test

Finally, test the server with server.sh script:

The output will be as below:

That means that you created server-side successfully.

Please keep reading Part 2 to know how to code the client-side with PHP Laravel 4.2

 

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