Wednesday, February 5, 2014

Setting up PhpStorm

Overview

I already covered how to create a Laravel project using the composer command line and looked a bit at setting up Sublime Text for Laravel development.

Now I'll do a quick overview of setting up PhpStorm to get Laravel, add dependencies, and briefly go over enabling debugging.


Installing Required Components

For this tutorial, you're going to need three things:

Git : for downloading Laravel

Composer : to download dependencies

PhpStorm : a PHP IDE


Installing Git

First you need to install Git to be able to check out Git repositories.

You can download Git at git-scm.com/downloads

Download the Windows Installer and install Git.

NOTE: During installation, choose Run Git from the Window Command Prompt to put Git in your system path or PhpStorm won't be able to find Git.


Installing Composer

If you followed the Getting Started from Scratch tutorial, you should already have composer installed.

If not, download Composer from getcomposer.org

You can either use the Windows Installer, or download composer.phar manually.

If you download composer.phar manually, make sure to put it somewhere where you can access it from your project folder.

Either put it directly in your project folder, or in a folder that is in the system path.

If you used the windows installer, composer.phar should be in

C:\ProgramData\ComposerSetup\bin


Installing PhpStorm 7.x

You can find a 30 day trial version of PhpStorm at www.jetbrains.com

The latest version at the time of writing is Version 7.1.2

Download and install the trial version.


Download Laravel using Git in PhpStorm

Now that you have the pre-requisites, we'll create a Laravel project completely from within PhpStorm.

We'll use Git instead of Composer to get our Laravel files.

Run PhpStorm

In The Welcome Screen select Check out from Version Control

Choose Git

Enter the Laravel repository URL : https://github.com/laravel/laravel.git

Choose a folder and name for your project

Click Clone

Now your Laravel files are downloaded, but you still need to pull in all the Laravel dependencies.

Like normal, we'll use the Composer command line for this step.

Open a terminal window: View > Tool Windows > Terminal

Do a composer update as normal

composer update

Wait for the Laravel dependencies to download.


Adding Composer Dependencies with PhpStorm

We already saw how we can pull in dependencies using the Composer command line.

Now, we'll use Composer in PhpStorm to add a dependeny.

I'll go through getting the Laravel Generators from within PhpStorm.


Adding Laravel Generators

Select Tools > Composer > Add Dependency

Select your composer.phar file and click OK. (See above for locating the file)

Search for the dependency, in our case: way/generators

Select the dependency and choose a version. I'll choose dev-master

Click Install

Wait for the dependency to download.

When finished, if you open your composer.json file you'll notice that the dependency has been added to the file.

NOTE : Remember to add the service provider to config/app.php

app/config/app.php
'providers' => array( 
 'Illuminate\Foundation\Providers\ArtisanServiceProvider',
 .
 .
 .
 'Way\Generators\GeneratorsServiceProvider', 
),

Setting Up the Debugger

Finally, we'll enable XDebug so we can debug our applications in PhpStorm.

Artisan Server

So far, I can only get the debugger to work when NOT using the Artisan server.

Therefore, I can't just go to localhost:8000, but I have to type in the full path to my project index.


Enable XDebug

Run wamp

Click on the tray icon and choose PHP > php.ini

Find XDEBUG Extension and change it to look like this:


; XDEBUG Extension

zend_extension = "d:/wamp/bin/php/php5.4.12/zend_ext/php_xdebug-2.2.3-5.4-vc9-x86_64.dll"

[xdebug]
xdebug.remote_enable = 1
xdebug.profiler_enable = 1
xdebug.profiler_enable_trigger = 1
xdebug.profiler_output_name = cachegrind.out.%t.%p
xdebug.profiler_output_dir = "d:/wamp/tmp"

Note : You don't have to enable the profiler, but make sure remote_enable is on.

You can also simply change these settings from the menu options in wamp, but I generally tend to change the ini files directly. Just to be sure.

Restart wamp.


Debug Configuration in PhpStorm

Now we need to add some configuration settings in PhpStorm.

Run PhpStorm

Open Project Settings. File > Settings

Choose PHP

Next to Interpreter click the ... button.

Click the plus button and choose the php.exe path.

Give it a name. I just use PHP 5.4.12

Apply and OK

Next, create a server.

Under PHP > Servers, click the plus button.

Name the server. I'll call it localhost

And the host is localhost

OK

Create a Debug Configuration

In the Debug Toolbar, click the dropdown and choose Edit Configurations

Click the plus button and choose PHP Web Application

Give it a name.

For the Server choose localhost

The Start URL will be the path of your project. Example: /Laravel_test/public

Choose a browser and OK

Now set a breakpoint to stop execution. I'll just set one in my routes file next to

return View::make('hello');

To set a break point, click on the empty area left of the code line.

From the Debug Toolbar choose the new configuration from the dropdown.

Click the little bug icon.


The app will execute in a new browser window and then stop at the breakpoint.

Now, in the Debug console, you can step through the source, see the values of your variables, step into function calls, etc.

To stop debugging, click the Stop button in the debug console.


Listen for connections

You can also listen for Debug connections. So, while navigating in the browser, if the code hits a breakpoint, execution will halt on you'll be able to debug in PhpStorm.

On the Debug Toolbar, click the Start Listen for PHP Debug Connection button.

Open your app in a browser. Example: http://localhost/Laravel_test/public/

It should stop execution at the breakpoint.

Remember to stop listening for connections when you're done.


Finished

I hope this is enough to get you going with PhpStorm. I know it's not as intuitive or lightweight as Sublime Text, but the debugging features more than make up for anything it might lack in speed.

Happy developing.


2 comments:

  1. simple and easy steps to setup Laravel on Windows, check it out here,
    http://justwebcode.blogspot.com/2017/07/how-to-setup-laravel-on-windows.html

    ReplyDelete
  2. Hi, very thanks for sharing advanced information... Your content is very effective for all user... I really like your post content and also your blog... Again Thanks for sharing... Your 100% Working Crack... PhpStorm Crack Provides all latest features...

    ReplyDelete