Posts tagged with tips

Supercharge Your Laravel Tinker Workflow

I ❤️ Laravel's tinker command and use it everyday. Caleb Porzio, an engineer at Tighten Co, wrote a good post on the that command, containing lots of cool stuff I didn't knew tinker could do.

Although some of the value Tinker provides is clear at first glance, it also has loads of hidden and exciting features available out-of-the-box. Let’s walk through and take a look at some ways you can super-charge your Tinker workflow.

https://blog.tighten.co/supercharge-your-laravel-tinker-workflow

Read more

A collection of PHPStorm tips

Nikush Patel created an awesome little site where he shares PHPStorm tips. Every tip is demonstrated by an animated gif.

I'm a big fan of PhpStorm and an equally big fan of keyboard shortcuts and optimised workflows, so I wanted to share all the best tips and tricks I know to help everyone else make the most of PhpStorm as well!

I produce useful tips in bite-sized gif recordings so they are easier to consume than reading the docs.

http://phpstorm.tips/

Read more

Join thousands of developers

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages.

No spam. Unsubscribe anytime. You can also follow me on X.

Overriding Laravel's helper functions

Miklós Galicz posted a short article on how he managed to override Laravel's str_slug helper function.

Long story short, until this is resolved one way or another... A really obscure but powerful tool can be a temporary solution for this. It's called Helper Overloading. Laravel's helpers are created in a way that checks if the method already exists. ... This is really great, the only thing remaining is to actually add our own method before Laravel creates it own version.

https://blackfyre.ninja/blog/fixing-slug-generation-problems

Read more

Things I didn't know Laravel could do

Matt Stauffer has been working on a book titled "Laravel: Up and Running" which will be released soon. In a post on his blog Matt shares a few hidden Laravel gems that he discovered while writing his book.

No blog post could contain all of the new things I learned from writing this book. I've been using—and teaching about—Laravel for years, and I was still shocked by how many tools and helpers and features I discovered.

Here are a few that stand out to me that I had never seen prior to writing the book.

https://mattstauffer.co/blog/things-i-didnt-know-laravel-could-do

Read more

Some awesome composer tricks

Composer really needs no introduction. At this point the PHP community pulled in billions of packages. Here are some Composer options that are not so well known.

You can view the versions of all the packages in your project by running composer show -i. Let's try it out in our Blender Laravel template:

composer -i

Want to see all the dependencies of the installed packages in a tree? Then run composer show -t:

composer -t

If you need help using a specific package then you can open it's documentation in a browser using composer. Try running composer home spatie/laravel-fractal to see it in action.

Know some other nice Composer tricks? Let me know in the comments below.

Read more

Some Laravel Homestead tips

Homestead is a pre-packaged Vagrant box that includes a good development environment. It was made and is maintained by Taylor Otwell, the creator of Laravel. In this post I'd like to share some tips regarding this box.

Map all sites at once

For every project a directory needs to be mapped from the host to the guest in folders-section of the yaml file. Let's make that a bit easier. Instead of adding a specific project to the folders-section you can add the folder where all projects reside in. I personally store all sites I'm working on in the `~/dev/sites` folder. So by adding that folder to the yaml file, no new mappings are needed when creating a new site.
folders:
    - map: ~/dev/sites
      to: /home/vagrant/sites

Avoid having to edit the hosts file

When adding a site to homestead you have to map an url to a public folder in the sites-section of the yaml-file. To make this work the url should be pointed to the ip address of the homestead box by adding it to your hosts file:.
192.168.10.10 url-to-your-homested-project.com  #default ip of the homestead box

You can avoid having to edit the hosts file by using a xip.io-url. Xip.io is a special domain created by Basecamp. The nameserver of that domain will resolve all urls to the ip-address that is specified inside the url. For example: "myproject.192.168.10.10.xip.io" will resolve to 192.168.10.10. If you use such an url in your homestead configuration there's no need to edit your hosts file.

sites
    - map: spatie.192.168.10.10.xip.io #no need to put this url in your hosts file
      to: /home/vagrant/sites/spatie.be/public

The downside of using a xip.io-url is that such url's are quite long. This can be improved by installing a local dns server. The local dns server will resolve all lookups of a given top level domain to the ip address of the homestead box. Here's a nice article that explains how to resolve all *.dev requests to a specified ip adress.

On OSX these steps can be used to install and configure dnsmasq to point all *.dev requests to homestead.

# Install dnsmasq
brew install dnsmasq
# Copy the default configuration file.
cp $(brew list dnsmasq | grep /dnsmasq.conf.example$) /usr/local/etc/dnsmasq.conf

# Copy the daemon configuration file into place.
sudo cp $(brew list dnsmasq | grep /homebrew.mxcl.dnsmasq.plist$) /Library/LaunchDaemons/

# Start Dnsmasq automatically.
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
echo "address=/dev/192.168.10.10" >> /usr/local/etc/dnsmasq.conf

#restart dnsmaq
sudo launchctl stop homebrew.mxcl.dnsmasq
sudo launchctl start homebrew.mxcl.dnsmasq

#make osx use dnsmasq
sudo mkdir -p /etc/resolver
sudo tee /etc/resolver/dev >/dev/null <<EOF
nameserver 127.0.0.1
EOF

After performing these steps you need to restart your mac. If you know some better way to active dnsmasq besides rebooting, let me know.

When your mac comes back up try pinging a random .dev domain.

Screen Shot 2016-01-13 at 21.24.30 With dnsmasq up and running you can .dev domains can be used in the homestead yaml file:

sites
    - map: spatie.dev #no need to put this url in your hosts file
      to: /home/vagrant/sites/spatie.be/public

Use a bash function to work with a globally installed homestead

You can opt to use a seperate homestead box for every project. I personally prefer one homestead box for all projects. In the Laravel 4.2 days the Laravel documentation covered a PHP tool to easily work with a homestead box. This section has been removed in the 5.X documentation, so I assume that development of the tool has been stopped. Fortunately all functionality that the PHP tool provided can be performed by this small bash function:
function vm() {
   cd ~/homestead
   
   command="$1"

   if [ "$command" = "edit" ]; then
      open ~/.homestead/homestead.yaml
   else
      if [ -z "$command" ]; then 
         command="ssh"
      fi
   
      eval "vagrant ${command}"
   fi

   #switch back to directory where command was performed in
   cd -
}

When this function is loaded these commands can be executed from every directory.

  • `vm up`: start the homestead machine
  • `vm halt`: stop the homestead machine
  • `vm`: ssh into the homestead machine
  • `vm edit`: edit the yaml file in the default text editor
  • `vm provision`: provision the homestead machine

Map your dotfiles directory

Terminal users often save their most used functions, aliases and general configuration in a dotfiles repository. This dotfiles-directory is generally cloned in the home folder on a development machine. When that directory is mapped to homestead as well the custom functions and aliases can be used from inside homestead.
folders
    - map: ~/.dotfiles
      to: /home/vagrant/.dotfiles

Syncing the folder is not enough. The steps you need to perform to load the dotfiles on the host machine should be executed in homestead as well. In case of my dotfiles, the minimum that should be done is symlinking the .zshrc-file

ln -s $HOME/.dotfiles/shell/.zshrc $HOME/.zshrc

This is the result when using my dotfiles: [caption id="attachment_1937" align="alignnone" width="1224"]My custom zsh promt is visible inside homestead. The "a"-alias (short for "php artisan" is working) My custom zsh promt is visible inside homestead. The "a"-alias (short for "php artisan" is working)[/caption]

Do you have some homestead tips to share? Let me know in the comments below.

Read more

Add a JavaScript breakpoint programmatically

When working on JavaScript code you'll probably find yourself riddling the code with console.log-statements when something is not working the way that you're expecting.

But did you know that there is a debugger statement available? It has invokes any available debugging functionality. To put it otherwise: you can programmatically set a breakpoint for your debugger. It should work in any browser.

function potentiallyBuggyCode() {
    debugger; //the debugger wil stop here
}

Here's the documentation on the debugger-statement on MDN.

Read more