Code Chewing Guides

PAGE 4 OF 7
  • How to debug PHP with PHPStorm & MAMP

    I struggled immensely one evening to find a tutorial out there that worked for me, on how to debug PHP applications running over MAMP, using the PHP Storm IDE. After absorbing bits and pieces from other guides, what really made it click for me, was reading the official xdebug documentation, where they provide a diagram of how the debugging components communicate, and what some of the xdebug settings mean.

    To point out - it doesn't really matter that the PHP server in this scenario is MAMP, this configuration will work for any 'remote' PHP server. I put remote within inverted commas, because the server is actually running on my local machine, but is considered remote for the implementation.

    What I had going on as my dev environment (but don't let the exact details put you off, as the flow should be the same):

    • MAMP 3.5, free version, running PHP 5.6.10 (did someone say PHP 7 was out?)
    • Mac 10.10.5 (OS X Yosemite)
    • PHPStorm (tested on 7.1.3 and 8.0.3)

    Without further ado, let's begin this winning guide, which by the way, was surprisingly straight forward to get working.

    Read more
  • Get the rendered PHP blade template content in Laravel 5

    There was a scenario I recently encountered, whereby it was especially useful to retrieve the rendered contents of the PHP blade template in Laravel 5, before simply returning it to the browser. I wanted access to the HTML view as a string, that I could further manipulate myself, before returning it to the browser.

    Let's jump right in and say that our PHP blade template in Laravel lives at resources/views/sample.blade.php, and this is what the contents are made up of:

    <div>
      <p>This is {{$name}}.</p>
    </div>
    

    We've got a simple route in app/Http/routes.php, which calls a method from our controller:

    Route::get('/whoami', 'SampleController@getName');
    
    Read more
  • Dynamically reference WordPress site URL in JavaScript

    During my WordPress development, I often find myself in need of the WP home or blog URL from within the JavaScript code. You can easily enough hardcode this to something like (assuming the WP site is at the root directory):

    if (somethingIsTrue) {
      location.href = '/some-page';
    }
    

    Or maybe your WP site is within a sub-folder, so you replace '/somePage' with '/wpsite/somePage'.

    This will be okay in a lot of scenarios. However, sometimes your WordPress site is not always situated at the same directory across different development environments. Dev and production environments might not match URL structure. This is a common pitfall when using the above technique.

    In other words, this isn't going to be a robust solution at all. And we're better than that.

    Read more
  • Conditionally assign value to a variable in bash

    Goal: A bash script that accepts one argument. If the argument isn't provided, then set a default value.

    This is the kind of conditional assignment I wanted to avoid:

    #!/bin/bash
    
    name=default
    if [ ! -z "$1" ]; then
      name="$1"
    fi
    
    echo $name
    

    There's nothing bad with this, I just felt it was a more verbose outcome than what I had in mind. I wanted something that more closely resembled a ternary operator, which leads us nicely onto the following variations…

    Variation #1

    Read more
  • Disable & remove virtual host website entry in Apache

    If your Apache 2 server is no longer hosting up a particular website, you can disable a virtual host entry on your server.

    Prep

    SSH into your server:

    $ ssh user@server
    

    You can list the currently enabled websites if you want to check the name:

    Read more
Code Chewing Twitter