Category Archives: General

The general entries.

Laravel Nested Resource Group

When I started with Laravel, nested resource group was available. I liked it because I can easily create a beautiful URL with ease. For example, to generate an URL like this/user/1/blog/10, I just need to nest my route with this:

Route::resource('user.blog', 'BlogController');

and my controller will be

class BlogController
{
    public function show(User $user, Blog $blog) 
    {
        //... 
    }
}

Since 5.2, this option is removed from documentation (but still available to use). So I was wondering why and I found someone already spotted this and submit a merge request. However, it was not merged with the reason below given by @taylorotwell.

Interesting…

OK, no problem. I will take his word for it. Since you can archieve something similar with alternative approach, e.g.

Route::post('user/{$user}/blog/{$blog}', 'BlogController@store');
Route::resource('blog', 'BlogController')->except(['store']);

but… I still want to know why it is not a good idea? I want to find out… so I can become a better developer.

Is it because of security? This is the first thing I come of mind. Assuming the URL meant to let user store or view their own blog, e.g. user Alice with user id 1, and she has blog entries 10,11,12. Does it mean if she access URL user/1/25 will be a problem? I don’t think so given if Policy is setup properly.

The other thing I can think of is keeping the route simple. It is because if your model route key name is not its ID but a customized route key name, the URL will be very long. e.g., using the route example above, if the user model route key name is user’s name and the blog model route key name is the blog title, the route will be user/alice/blog/this-is-my-title-of-my-blog. It is definitely longer than blog/this-is-my-title-of-my-blog.

There must be some disadvantage of using nested route, what do you think it is?

Managing Virtual Machines

I use multiple two virtual machines that were built with Vagrant. It is a pain most of the time to shutdown both of them when I am done for the day.

I wrote the scripts to shutdown all the running virtual machines with one command.

PHP Package Development

I really enjoy this course. It not only covered the PHP package development but also covered topics beyond that.

For example, it explains the versioning, CI, style CI, importance and guide of creating a README, license file, and change log.

It helps me to get started building a package for PHP and at the same time, I can use the lessons I learn as a checklist to make sure the package is complete.

Some of the links from the lessons, I put them below for my future reference.

https://packagist.org/

https://phpunit.de/

https://semver.org/

https://travis-ci.org/

https://styleci.io/

https://www.makeareadme.com/

https://choosealicense.com/

https://keepachangelog.com/en/1.0.0/

Jet Lag

I recently traveled to Asia for vacation. It was located at a 16 hours different time zone from home base.

It was not my first time travelling to Asia and I always had a hard time adjusting to the new time zone when I was there and when I returned home.

This time, I did something different and it helped me a lot.

On the first two days upon arriving and returning, I slept whenever I felt tired. I sleet at 3pm local time and woke up at 1am local time, but it’s OK. I was not forcing myself to adjust to the local time, but let my body dictate the sleeping pattern.

On the third day, things will go by easily and much easier to adjust to the local time.

It sounds like a waste of time on the first two days, but I did enjoy the rest of the trips.

Homestead + dnsmasq

Homestead and dnsmasq

In Laravel, there are two out of the box setups. One is using Homestead and the other one is using Valet.

I personally prefer using Homestead because it is entirely on a virtual machine and it does not affect the host OS. If anything goes wrong, I can simply just destroy the Homestead virtual machine and start a fresh box.

One of the annoyance to me using Homestead is whenever I create a new project and giving it a unique URL, e.g. project-1.example.test, I have to edit the /etc/hosts file and add the domain to the list in order for the new URL to work.

Instead of editing the hosts file each time, I started using dnsmasq to proxy all the example.test to the IP address of the Homestead machine.

The setup is straight forward, here is my steps.

  1. brew install dnsmasq
  2. mkdir /etc/resolver
  3. touch /etc/resolver/example.test
  4. Edit /etc/resolver/example.test and add nameserver 127.0.0.1 at the top of the line.
  5. Edit /usr/local/etc/dnsmasq.conf and add address=/.example.com/<your homestead box ip> in somewhere beneath #address=/double-click.net/127.0.0.1.
  6. Start dnsmasq with sudo brew services start dnsmasq