Sunday, May 31, 2015

Preparing for drupal 8

Why now?

It look like the drupal 8 betas are going to end soon. Last month there still were 40 critical bugs, it looked like there was no progress. But with beta 11 the critical bug count got cut in half!

This was a sign for me to get up to speed with the last developments. For that i'm using the great talk by Larry Garfield at last years drupalcon in Amsterdam.

Going through the motions

I have downloaded beta 11 on my windows machine and i'm using xampp to run it.

He said a controller can return a string, this seems not true anymore. I got an error the return should be a Response instance.

Installing modules aren't mentioned in the talk, but I got a strange error enabling the custom module. It appears to be a mysql setting issue.

In the routing.yml file he doesn't quote the path value, but it seems a lot of other modules do. So I guess it is a best practice to do so.

When he makes the module themeable, he uses the template folder. This generates an error saying you have to use templates.

This must be an oversight, the BlockBase class is in the Core folder.

Observations

Drupal 8 is major evolution for the platform. As Larry mentioned even drupal 7 is based on php4 practices, when it is almost ten years ago that php5 came out. It is my biggest gripe and drupal 8 wipes it out.

I'm not that found on the mix of yaml files and annotations for configuration, but splitting up the configuration is a developers dream after the hooks for everything way.

In drupal 7 it would have been more difficult to find the errors, but with the modern architecture errors made it easy to get to the problem and fix it. Goodbye common.inc, we won't miss you.

They didn't only modernized the architecture, they also added new default modules and an improved way of doing things, that make it an amazing content management platform.

With the mordernized architecture come great tools, instead of the devel module people have ported the web profiler from the symfony framework.

Questions from great readers

Why don't you use drush?

It looks like drush has still a long way to go to support drupal 8 on windows.

You need to use drush 8 to have drupal 8 support. There are a lot of articles that say drush 7 supports drupal 8, but drush 7 mentions otherwise.

When you install drush 8 with composer, the only way at the moment, the drush command doesn't work in the command prompt because it has linux syntax. The drush.bat command doesn't work either of the same reason.
The drush.php.bat command seems to work if you use it without parameters, but once you use it with parameters it can't find the tasks.

There is an alternative, drupal console. It works on windows.

Why can't I uncheck the custom module, once it's enabled?

It seems like a security measure to prevent a compromised data integrity. You can uninstall modules, so the unchecking hasn't disappeared.

Saturday, May 30, 2015

Code doesn't care who wrote it

Only people care

Computers just don't care.

I just saw a github conf post, and I was as thinking who cares?

My view

For all I know the code could have been written by an alien from starcluster 5574564, if it's code that is readable or at least has a predictability I love the programmer(s) that wrote it.

I know programming looks like it is a boys world, but I'm almost 40 so I am not a boy and I still surprise the boys at my current firm.
It isn't all about knowledge if you are a real programmer. You have to analyse problems and translate that into code.

I can understand the sociological aspect of hiring a programmer as HR but that it is human nature. You hire the people you can connect with. A more technical person comes in to evaluate that aspect. And when it is all smoothied you get the conclusion.

Small victory

While I'm not against focusing on groups that are statistically are under represented in the programming community. But showing that there are amazing people out there that need to get hired is great.

I believe people who do good things will get noticed. I am a cook/waiter if you only follow my school education. I work in IT for a little than 10 years now. I always improved my knowledge and sharpened my problem solving skills.

I will always try to improve myself, it has nothing to do with work. it has to do with the person I want to be, better stronger faster smarter, until the day I die.

Sunday, May 24, 2015

PHP 7 spaceship operator

Wait what?

The spaceship operator isn't a name for a language that wants to get enterprise credibility.

Take a pause, and look at it for a distance

The operator probably solves a problem many php developers face.

stackoverflow

Instead of only having a boolean check, it is a ternary check without the logic you need to write yourself.

One thing php as a language was always good at is solving real world problems, sometimes with flawed implementations, but they make it easier for others to understand the code.

epilogue

Don't be square and make your code php 7 compatible as soon as possible.

Friday, May 22, 2015

Lazy and hungry at night

It's been almost a week i'm riding past the barrio cantina site, so I'm really curious if food trucks can become a thing in Belgium.

It's too late foor clicking on all the links to see the food truck descriptions, so i open web tools and type in the console

Now I just read and dream of the food I will get in a few hours.

Saturday, May 16, 2015

HTML up and down

I just found a html to json parser, himalaya, and i though this needs a json to html parser.

It isn't fancy, but it gets the work done and it's readable.

Friday, March 27, 2015

How not to prove your php skills

I'm always interested learning about ways of doing things. So when I came across an article about routing in php I was curious.

And then I saw the code.

32 lines to get one route?

The getCurrentUri function does 'magical' things with the SCRIPT_NAME and REQUEST_URI server variables.
The first thing I thought of was why don't they use the parse_url function?

And then it got worse on line 14 and 15, two $routes variables?
The idea is to get a array of the url segements. Line 15 is enough.

Now we are getting to the meat, the actual router. It reminds me of the time I used an asteriks in sql queries and referred to the fields as numbers, because I'm too lazy to type their actual names.

Because I was so outraged by this abundance of code, I wrote my own.

On line 3 you see how I get the path from the server global. For some reason PATH_INFO isn't set anymore, but REDIRECT_URL has the same value.

And then I'm doing my magic. Looking for the /search/book segments, stripping them when found and echoing everything that comes after them.

From 32 lines to 3, that is amazing!

PS: I commented on the article first but that was two weeks ago. My voice must be heard!

Sunday, March 15, 2015

How (not) to use SPA software

I see more and more solutions on what shouldn't be a problem in the first place, SEO of pages.

As a developer I understand wanting to use the new and shiny things. But that is a poor excuse to make them an hammer, because everything is a nail.

Google is getting a lot of attention with its angular SPA framework. It's strange that they as an search engine started to guide people to misuse angular by adding escaped_fragment to the url for crawlable content.

Because it's a google only solution people turned to server rendered SPA content, an example. The author of the example uses the term isomorphic javascript in a too narrow definition, but that is another discussion.

The keyword in the acronym is application. An application by definition is dynamic and therefore SEO is not an issue.
A website is static, don't be fooled by the dynamic backend, and SEO is very important.

So don't make an application of a website.

Tuesday, February 10, 2015

An alternative to the laravel 5 contact form

Introduction

I was reading the contact form tutorial, and after that I'm trying to get the tutorial working on my pc.

There was a bit of information missing, but that's because the writes wants you to buy his book. And there are a few ways the code can be written different.

Get on your marks, GO!

If you don't know how to install laravel, check the excellent documentation.
To run the development server, run php artisan serve in the command line.

alternative routes

Laravel has a way to prefix the controller methods with the http verbs, they call it implicit controllers.

In the file app/Http/routes.php you add the line Route::controller('contact', 'ContactController');.

To create the controller, run php artisan make:controller ContactController --plain.
The --plain parameter creates the controller without methods.

The controller

You can ignore line 5 and 6 for the moment, they will be created later.

Add the lines 10 to 23 to your controller.

To get the getIndex method working the index.blade.php file needs to exist.

index.blade.php

This file has to be created in the resources/views/contact directory

The missing information in the original article is to make the Form methods work.

  1. Add "illuminate/html": "5.*" to the required section of the composer.json, and run composer update
  2. Add 'Illuminate\Html\HtmlServiceProvider', to the config/app.php file in the providers array.
  3. Add 'Form'=> 'Illuminate\Html\FormFacade', to the file above in the aliases array

intermezzo

If you put the postIndex method of the controller and lines 5 and 6 in comments. And start the development server, you will see the contact form at http://localhost:8000/contact.

ContactFormRequest

run php artisan make:request ContactFormRequest.

On line 14 you change false to true, because you want anonymus users to send the contact form.

In the rules method you add the items from the code above.

ContactFormValid event

The original article lets you add the mail sending code to the store controller method. This gives the controller to much responsibility. I will use an event to trigger the mail sending.

In the app/Providers/EventServiceProvider.php listen array you add following item;
'App\Events\ContactFormValid' => ['App\Handlers\Events\EmailContactForm', ],

As you can see the event itself is in the app/events directory and all the code that needs to be executed is in the app/Handlers/Events directory.

Then you run php artisan event:generate.

ContactFormValid.php

As you can see in the controller on line 18 the ContactFormRequest instance is added to the constructor.
In the event file the all method is called to fill the data variable.

EmailContactForm.php

On line 2รง you see the data variable is added to the Mail send call.

Because it is an array you can use this handler for other events, the only thing you need to check are the data keys and their value.

contact.blade.php

In the handler on line 28 we defined this view file, which is in resources/views/emails directory.

The big finish

Remove all comments from the controller and fill the contact form in your browser.

Now the controllers only function is to display a view. And when the form is posted with valid data it fires an event and redirects the user to the form.

Saturday, January 31, 2015

No more static site generators

Preface

I read the article Getting started with web components and I thought how can I create a product with this knowledge. A static generator came to mind because the solutions out there want to fix the problem of reusing html snippets.

The workhorse

If you don't want to browse the code, just scroll down to see the usage.

Usage

To create a page with reusable parts you have to at least create the page itself and the reusable part.

On line 3 you see the link to the reusable part.

On line 6 I added the tag which will contain the content of the reusable part.

On line 7 there is the link to our workhorse.

On line 9 the addTemplate function to add a specific reusable part to the page

The specific reusable part in this case is;

The content of the reusable is inside the template tag. And by convention the id of the template tag has the same value than the filename. Developers will recognize this pattern, because they use it for classes.

On line 73 of the workhorse code you can see there is also a addHeader function. It works as follows:

  • There needs to be a header tag on the page.
  • The header.html needs to exist and added to the import links of the page.
  • When you call the function it has to have a title parameter

To get a better idea of how it works you can check the demo.

Is this a dream?

let me say it's a very lucid dream.

The code is more proof of concept than a product I want to ship.

At the moment the only browsers this will work on out of the box are chrome and opera.

All in all I think this could become a product with a lot more work.