Laravel Nova with your pipelines updated

Standard

Shortly after I added ‘Laravel Nova with your pipelines‘ I stumbled across a tweet on using an environment variable with composer. So instead of maintaining an auth.json file in the repository and using sed to replace placeholders, you can just add a COMPOSER_AUTH environment variable with the entire contents.

GitLab environment variables page

There had to be a better solution and here it was. Nice and simple.

Laravel Nova with your pipelines

Standard

If you use Laravel Nova, you undoubtedly were happy when Taylor started allowing us to use composer to install and update Nova. By doing so you create or edit your ~/.composer/auth.json file with something like the following:

{
    "http-basic": {
        "nova.laravel.com": {
            "username": "your-email@your-email.com",
            "password": "your-super-secret-token"
        }
    }
}

On your dev machine you need this file, along with your production or wherever you are building / deploying your site. Using forge, this is a simple git pull and not a complicated deployment.

I setup a GitLab pipeline so that when pushing the master branch, it will run my unit tests, then trigger forge to do the deployment.

To get composer to install and not error out means we had to add auth.json to our repository and in our build steps, copy it to the correct directory. By using variables, we don’t need to store our credentials in that file. Our auth.json is literally what we have above. We are going to replace your-email@your-email.com with our custom variable in the Gitlab CI/CD Environment Variables page.

GitLab CI variables

Here is what our test stage looks like. The ‘- sed’ lines replace our placeholders in auth.json with the variables we defined above.

phpunit:

  stage: test

  script:
    - cp auth.json ~/.composer/auth.json
    - sed -i "s/your-email@your-email.com/${NOVA_COMPOSER_EMAIL}/g" ~/.composer/auth.json
    - sed -i "s/your-super-secret-token/${NOVA_COMPOSER_TOKEN}/g" ~/.composer/auth.json
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts
    - cp .env.testing .env
    - php artisan key:generate
    - phpunit --colors=never

There may be a more elegant way to achieve this, but this works and doesn’t jump through too many hoops.

Tailwind CSS, I try it out on the TodoSage website

Standard

When building websites it’s so easy to just grab Bootstrap or Foundation and take off running. Better yet, grab a theme off of one of the many theme sites who integrate bootstrap for you and go. Tailwind CSS has always been in the back of my mind as I have a few courses from the author and the people I follow on twitter are friends with them.

While I wasn’t happy with the TodoSage.com site, I decided to give Tailwind CSS a try. I have looked at it a few times but only had big projects that I could use it on. Basically it was just too big of a leap for me to take at the moment.

One of my goals before the new year was to replace the Todo Sage site with a new one that included screenshots. In doing so, I decided to give Tailwind a try as it’s just a few pages.

Tailwind CSS Homepage
Tailwind CSS Homepage

I have done ‘utility’ style classes in many projects. .pl-15 (padding left 15), mb-10 (margin bottom 10) and much more. Tailwind as built as a utility framework and comes with a lot of classes. To make things easier, they have a config file where you can define colors, margins, and all sorts of things it uses to generate classes. This actually made it really easy to keep ‘on brand’ in the HTML. Here is a purple button, and on hover it should be ‘purple-darker’.

By removing the theme I used in the past, this will make it easier to iterate and try things out.

Things I like about Tailwind

There are a bunch of things I like about Tailwind.

  1. The configuration file is well documented with comments inside it. This makes it much easier to understand and doesn’t come across as overwhelming.
  2. It just looks good. If you look at the examples and work towards mimicking it at first, it comes out looking pretty decent.
  3. Once you understand the ‘formatting’ or ‘syntax’ of the classnames, it’s super easy to use and guess. If you have used ‘font-light’ it’s easy to guess ‘font-bold’.

Things I don’t like while using Tailwind

  1. The setup is a bit confusing. You can of course load their CDN version that gets their defaults. But to match what you need, you of course need a local install of it. They provide multiple scenarios, but the install page makes it look scary. I am never confident I have it working until I see that it is.
  2. This isn’t on Tailwind, as they provide reasonable defaults, but it’s easy to not remove the stuff you don’t need. The default config comes with black, grey, white, red, orange, yellow, green, teal, blue, indigo and purple. Most of those have darkest, darker, dark, (base), light, lighter, and lightest. If you don’t remove the colors you don’t use, you end up with a lot more classes than you need. This is more of a newbie mistake.
  3. The examples are beautiful and a great way to get started, but make sure to remove all the classes you don’t need on the elements. A section that just has text centered probably doesn’t need flex classes. Again, as a newbie mistake, it’s too darn easy to just grab and go.

Bottom line, I like Tailwind and the more I use it, the more I like it. All 3 things I don’t like about it will disappear as I become more familiar with it. It certainly made spinning up a restyle of TodoSage easier and not a 6 month process.

Not sold yet? Follow them on twitter.

Subdomain testing in Laravel

Standard

Learning about automated testing and implementing is great. Unfortunately some projects I work on are multi tenant, and each client has it’s own subdomain. I then have an “app” subdomain with separate routes on it for things like API, or switching tenants if you belong to multiple of them. You may find a few articles that say to just use $this->call() instead of $this->get() or $this->post(). It really is that simple.

I use a domain like lvh.me which points to 127.0.0.1 along with it’s subdomains.

Here is my setup:

App: app.lvh.me
Tenant App Example 1: test1.lvh.me
Tenant App Example 2: my-company.lvh.me

Example routes.php file

//app domain routes
$router->group(['domain' => 'app.lvh.me'], function($router) {
    $router->post('register', 'App\RegisterController@store');
});
 
//tenant routes
$router->group(['domain' => '{account}.lvh.me'], function($router) {
    $router->post('login', 'Tenant\LoginController@store');
});

To test the login route for the Tenant App Example 1 we do:

$this->call('POST', 'http://test1.lvh.me', ['email' => 'test@example.com', 'password' => 'mysecretpassword']);

To login to Tenant App Example 2, we would do:

$this->call('POST', 'http://my-company.lvh.me', ['email' => 'test@mycompany.com', 'password' => 'mycompanypassword']);

For routes on our App domain we do:

$this->call('GET', 'http://app.lvh.me/register');

PHP Mcrypt on OSX with XAMPP

Standard

HTML CodeWas composer not able to find the mcrypt PHP extension? When I developing on my local machine I use XAMPP because it is easy to setup. I also tend to use Laravel for a lot of projects as well. If you are using the same, when doing a composer update you have no doubt encountered this error. Mcrypt PHP extension required.

Writing lock file
Generating autoload files
Mcrypt PHP extension required.
Script php artisan clear-compiled handling the post-install-cmd event returned with an error

It turns out there is an extremely easy way to fix this. Simply update your .bash_profile with the following text:

export PATH=/Applications/XAMPP/bin:$PATH

This will find the XAMPP version of PHP that has mcrypt installed first instead of the one that comes with OSX.

The .bash_profile file allows you to have user defined settings when using Terminal. If you want to user a different version of PHP, you can change it here. Do you want to setup shortcuts? A lot of people will create a shortcut so that ls -l can be run by typing ll.

To do that enter the following in your .bash_profile file.

alias ll="ls -l"

If for some reason you don’t have a .bash_profile, create it in your root user directory. If your username is jeff, the file will be /Users/jeff/.bash_profile.

After you have made your change you may need to source your profile. Execute the following command inside terminal to do it.

source ~/.bash_profile

Once this is done, you should be able to do a composer update without any errors occurring.

Image courtesy of Baitong333 / FreeDigitalPhotos.net

Deploying Laravel with Capistrano

Standard

I am always looking for better ways of deploying me code. In the past I have used Fabric to create a deployment process. Although I use git, I have never used it for deployments. With Fabric I copied my code to another folder, did any processing such as removing files, or setting a environment variable and then uploaded the package. I was able to create versioning. Each deployment went to it’s own folder and symlinks to the document root were created. In the event the deployment gets botched I can easily login and re-link to the previous deployment.

On a new project I recently launched I have started using Capistrano. If you are a Laravel user check out this tutorial from TutsPlus.

Using Capistrano, I can run a command and it will rollback for me. With the default setup I have also switched to git based deployments. Both GitHub and Bitbucket allow you to enter deployment keys for your server. These are SSH keys but have read-only access to pull down the changes.

What I like with this approach is that the client side deployment script only relies on having access to the server. No longer am I defining paths where to copy files to if I have to deploy from my laptop versus my desktop.

What I don’t like is the “config” folder in my root project. It’s quite vague but I haven’t looked to see if I can change the name. One thing I am looking to do is write the release name to a php file so I can use it for things like cache busting.

Podcasts I Listen To

Standard

I like talk radio. In the mornings I listen to a morning show instead of music. At work I listen to several podcasts as it helps me focus. Unfortunately I end up binging on them so most days there isn’t a new episode to listen to. Here is the list of podcasts I currently listen to.

Radiolab * This is by far my favorite.
Jay & Silent Bob Get Old
Tell ‘Em Steve-Dave
Laravel.io
Improve Photography
Larry Uncensored Podcast

I also run ActivePodcast which is a free podcast discovery network.

What podcasts do you subscribe to?