POST CATEGORY : PHP
বাংলা স্ক্রীনকাস্ট – S.O.L.I.D ডিজাইন প্রিন্সিপলস
The Video is available in HD
বিদ্র: হঠাৎ লোড শেডিং এর কারনে 00:29 এর কাছাকাছি থেকে 5:20 এর কাছাকাছি পর্যন্ত সময়ে সাউন্ড কোয়ালিটি খারাপ হওয়ার পাশাপাশি একটি বিরক্তিকর নয়েজ তৈরি হয়েছে । এই সমস্যার জন্য আমি আন্তরিক ভাবে দু:খিত
Using Laravel Queues Standalone (Outside Laravel)
The title is pretty self explanatory. We know how to use Queues inside Laravel. But Laravel components are also usable outside Laravel itself. Today we’re going to see how to implement our own workers on top of the “Illuminate\Queue” package.
Installing the required components:
What do we need?
In our example, we shall be using “Beanstalk” for message queueing. So please make sure you have it installed. This is how to install it on OS X using homebrew:
1 |
brew install beanstalk |
Then we need to install these following PHP packages. We can use Composer to install them from the Packagist repository.
- “illuminate/queue” – the queue component
- “pda/pheanstalk” – we shall use the beanstalk driver, so we need the beanstalk client
- “illuminate/encryption” – some drivers need the encryption class
So this how our composer.json looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{ "name": "masnun/testqueue", "authors": [ { "name": "Abu Ashraf Masnun", } ], "require": { "illuminate/queue": "5.0.28", "pda/pheanstalk": "~3.0", "illuminate/encryption": "5.0.28" } } |
Now we need to make sure that all the required packages are installed.
1 |
composer install |
Let’s write the codes:
We first create a file named “loader.php” which will contain the common part. In this file, we shall construct a Queue object and add connection to Beanstalk. Here’s the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php require_once 'vendor/autoload.php'; use Illuminate\Queue\Capsule\Manager as Queue; $queue = new Queue; // Some drivers need it $queue->getContainer()->bind('encrypter', function() { return new Illuminate\Encryption\Encrypter('foobar'); }); // Configure the connection to Beanstalk // Note: The second parameter is the connection name // We also define a queue name // We will need these two names later $queue->addConnection([ 'driver' => 'beanstalkd', 'host' => 'localhost', 'queue' => 'default', ], 'default'); |
Let’s say we shall have some emailing tasks which would send email to our users. Let’s create a new file named “handlers.php” and create a class named “SendEmail”. This class must have a “fire()” method which can take the Job instance and data as arguments. Here’s sample codes:
1 2 3 4 5 6 7 8 9 10 |
<?php class SendEmail { public function fire($job, $data) { echo "Sending email to: {$data['email']}" . PHP_EOL; } } |
OK, we just created our awesome email sending class. Our task handler is ready. Now we need to create workers which would keep running and handle tasks from the queue. We create a sample worker named “worker.php”:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php require_once 'loader.php'; require_once 'handlers.php'; use Illuminate\Queue\Worker; $worker = new Worker($queue->getQueueManager(), null, null); // Run indefinitely while (true) { // Parameters: // 'default' - connection name // 'default' - queue name // delay // time before retries // max number of tries $worker->pop('default', 'default', 0, 3, 1); } |
We have handlers written and workers ready for some action. Let’s launch the worker:
1 |
php worker.php |
The workers would fire up and keep listening to the queue. Whenever a new task would arrive, it would take it off the queue and process it. So now we need to queue some tasks. Let’s do that now:
1 2 3 4 5 6 7 8 9 10 11 |
<?php use Illuminate\Queue\Capsule\Manager as Queue; require_once 'loader.php'; // Make this Capsule instance available globally via static methods... (optional) $queue->setAsGlobal(); |
Now if you look at the terminal window where you ran the worker, you would notice the output from the job handlers.
পিডিও, মাইসিকুয়েল ও পিএইচপিমাইএ্যাডমিন
The Video is available in HD
Moving Your PHP Projects to Vagrant
If you have read my last blog post, I have chosen to use one Linux box to locally develop web applications. In the last post, I discussed how I setup the Python/Django environment. In this one, I’m going to talk about PHP.
As usual, I installed the LAMP server:
1 2 |
sudo apt-get update sudo apt-get install lamp-server^ |
I have all my PHP projects in “~/Sites”, I wanted to serve them from there. So removed the www directory.
1 |
sudo rm -rf /var/www |
Then I added my ~/Sites directory as a synced folder to be mounted as /var/www in the linux guest box. www-data is the user and group permission on these directories so Apache has no issues handling them.
My updated vagrant file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# -*- mode: ruby -*- # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.synced_folder "~/Sites", "/var/www", :owner => 'www-data', :group => 'www-data' config.vm.box = "precise64" config.vm.network :private_network, ip: "192.168.234.131" config.vm.hostname = "tardis.dev" end |
Upgraded PHP to PHP 5.5 and installed PHPMyAdmin:
1 2 3 4 5 |
sudo apt-get install python-software-properties sudo add-apt-repository ppa:ondrej/php5 sudo apt-get update sudo apt-get install php5 sudo apt-get install phpmyadmin |
Updated my /etc/hosts to update the local dev domains to the IP address of the vagrant box 🙂
1 |
192.168.234.131 tardis.dev |
Debugging & Profiling PHP Apps with xDebug – Bangla Screencast
Video is available in HD
Laravel 4 Development on PHPStorm
Laravel & PHPStorm – the best of the two worlds. Laravel is the most popular PHP web framework today. PHPStorm is the best PHP IDE that money can buy. What’s sad is that there is still no built in support for Laravel in PHPStorm. I created an issue on YouTrack (Jetbrain’s issue tracker) a long time ago which has received quite a good number of votes by today. I do hope Laravel support will come soon on the IDE. Meanwhile, there are workarounds to get things done smoothly.
Auto Completion
Thanks to the massive class aliasing (& use of Facade) in the framework core, PHPStorm can’t provide true autocompletion for Laravel by default. But there are excellent packages like Laravel IDE Helper which can generate phpdocs from the framework source. It generates a file which the IDE can parse and use the generated codes to provide autocompletion.
Installation and usage is simple. First modify your composer.json to include it in the require section:
1 2 3 4 5 6 7 8 9 |
{ // [snip] "require": { "laravel/framework": "4.1.*", "barryvdh/laravel-ide-helper": "dev-master" } // [snip] } |
Run composer update:
1 |
composer update |
The package should be installed if everything goes right. Now, you need to make sure the package is loaded in Laravel (so that artisan can execute the commands it provides). Add this to the providers array under app/config.php – ‘Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider’. It should look like:
1 2 3 4 5 6 7 8 |
'providers' => array( // [snip] 'Illuminate\Workbench\WorkbenchServiceProvider', 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider', ), |
Now you can do this in command line:
1 |
php artisan ide-helper:generate |
The command should generate a file named “_ide_helper.php” with the necessary codes. You might want to recreate the code cache from “File” > “Invalidate Caches / Restart”. When the IDE restarts, you should get code completion for most of the Laravel code.
Blade Syntax
This one could be a bit tricky. But if you have installed Textmate bundles into Jetbrains IDEs, it’s actually simpler.
(1) Download the textmate bundle here – https://github.com/outofcontrol/Blade.tmbundle. If you download the zip, uncompress it to somewhere on your harddisk.
(2) From PHPStorm’s Settings window, go to TextMate Bundles. Add the bundle and apply the changes.
(3) Go to “File Types” settings. Select “File types supported via textmate bundles”. Add “*.blade.php” to that list. Apply the changes. Now try and open a blade file. It should work.
(4) Additionally, if there’s horrible color scheme on blade files, go back to “Settings” > “TextMate Bundles”, assign the color schemes to Darcula. (In my case, I switched all to Dracula under the “TextMate Color Scheme” column. If you click an item under that column, you should get a pop up to select.)
Now the blade syntax should work fine with nice color scheme.
Apache on OS X: Fixing file system permissions for Symfony or Laravel
I use Apache2 on OS X for my PHP development. I work with both Symfony2 and Laravel. Both the frameworks needs certain directories to be writable. And chmod sometimes doesn’t work well (may be because I don’t understand the permissions on OS X well?). Whether it’s my fault or not, here’s quick help:
(1) Open up your httpd.conf and checkout the values for “User” and “Group”. In my case it is “_www” and “_www”.
(2) Let’s make them owner of the directory in question. In the case of Laravel 4, something like:
1 |
sudo chown -R _www:_www app/storage |
That’s “User:Group” combination for apache. Now everything should work fine.
JSON Handling in PHP – Bangla Screencast
Video is available in HD
Dependency Injection in PHP: Bangla Screencast
The video is available in HD
Related Resources:
- Source Code: https://github.com/masnun/php-dependency-injection-examples
- Pimple Homepage: http://pimple.sensiolabs.org/
- Pimple Github: https://github.com/fabpot/Pimple
- Pimple on Packagist: https://packagist.org/packages/pimple/pimple