Archive for the ‘Tutorial’ Category

How to format JSON in VIM/GVIM

Thursday, February 23rd, 2017

The strength and beauty of VIM is the simple interaction with lots of external programs.

To format any JSON file in VIM/GVIM:

:% !jq .

% means the file you’re working on, and `!jq` is run the external command `jq`

That’s all.

You need to have `jq` installed:
sudo apt install jq
whatis jq
jq (1) - Command-line JSON processor

Laravel 5.1 development with the Atom editor

Tuesday, June 30th, 2015

The brand new Atom editor is heavily inspired by Sublime, build as an open source project on webtechnology, so CSS, JavaScript and HTML, Coffeescript, LESS and NodeJS, it’s founded by Github and really hackable into the next generation.

It sports a package installer, and a nice repository full of plugins, more then 2100 at the moment.

It just released version 1.0, for Linux, Windows and OS-X. Atom is free and open source.

In short I like it. It has good support for syntax-highlighting, code-completion and snippets. A code beautifier is available as package, and so is Emmet. Atom is more an editor than a full-blown IDE, like Netbeans, but packages are really easy to build, so I guess, a lot of extending is possible.

For Laravel you need some tweaks.

Improve Laravel support in Atom

Install some extra Laravel related packages:

  • language-blade
  • atom-laravel

There are more packages like blade-snippets, but actually you don’t need them. Snippets are include in the language-blade package, although the documentation lacks to mention that. The documentation just says:

Blade templating support in Atom

Actually it adds snippets too.

Make Atom act as a IDE

A great feature of Atom is the build in Tree View. Open a projectfolder, and Atom will show you a nice tree folder view.
But to browse through your files, use another feature. Atom will index all files. CTRL P will open an findAll dialog, or a fuzzy finder and that is the fastest way to change and find files in your project. Much like Sublime or Netbeans, or CTRLP plugin for VIM.

Another IDE feature Goto Declaration doesn’t seem to work. And that is true, it doesn’t work out of the box. That did confuse me and apparently others. It needs a CTAG file. CTAG files are created by another program Exuberant Ctags. Atom’s approach is much like the Unix philosophy. Don’t invent the wheel, or

  1. Small is beautiful.
  2. Make each program do one thing well.

Exuberant Ctags does something well, so don’t imitate it. Use it.

How to install Exuberant Ctags for Atom support

sudo apt-get install exuberant-ctags

Then CD into your working directory, the root of your project:

cd ~/project

Then run CTAGS to create the TAGS file:

ctags -R --languages=php

That’s it. Goto Declaration will now work on Laravel helper functions and classes.

Great. Netbeans lacks support for Laravel, and especially editing blade files is much easier in Atom with the language-blade package.

Atom isn’t the fastest editor, it’s isn’t optimized yet that much according to the developers, and it is build upon web technologies. That means it’s hackable and extendible for nearly everyone. For that it already got a sweet spot here and no doubt the community will build every package you can think of. It´s so easy and well integrated with Github. Bright future.

How to localize a Faker database seeder in Laravel 5

Wednesday, June 24th, 2015

Database migrations and database seeding is supported out of the box in Laravel 5.x. A great feature. In a wink you get a database full of `faked` but structured content. You can even create random image-urls (lorempixel).

Strangely even if you set your Application Locale Configuration in `config\app.php` on “nl” you still get faked English names instead of Dutch. That is not what you want.

The fix is easy, suppose you have this ModelFactory:


$factory->define(App\Person::class, function ($faker) {
return [
'name' => $faker->name,
'company' => $faker->company,
'pic' => $faker->imageUrl(600, 400, 'business'), // 'http://lorempixel.com/600/400/business/'
];
});

Simply add another provider and `overwrite` the old English provider


$factory->define(App\Person::class, function ($faker) {

$faker->addProvider(new Faker\Provider\nl_NL\Person($faker));
$faker->addProvider(new Faker\Provider\nl_NL\Company($faker));

return [
...

That way the object will be extended, the new provider will be unshifted to the beginning of the providers array


Faker\Generator {#560
#providers: array:19 [
0 => Faker\Provider\nl_NL\Company {#583
#generator: Faker\Generator {#560}
#unique: null
}
1 => Faker\Provider\nl_NL\Person {#582
#generator: Faker\Generator {#560}
#unique: null
}
2 => Faker\Provider\Uuid {#580
#generator: Faker\Generator {#560}
#unique: null
}
3 => Faker\Provider\UserAgent {#579
#generator: Faker\Generator {#560}
#unique: null
}
4 => Faker\Provider\en_US\Text {#578
#explodedText: null
#consecutiveWords: []
#generator: Faker\Generator {#560}
#unique: null
}
5 => Faker\Provider\en_US\PhoneNumber {#577
#generator: Faker\Generator {#560}
#unique: null
}
6 => Faker\Provider\en_US\Person {#576
#generator: Faker\Generator {#560}
#unique: null
}
7 => Faker\Provider\Payment {#575
#generator: Faker\Generator {#560}
#unique: null
}
8 => Faker\Provider\Miscellaneous {#574
#generator: Faker\Generator {#560}
#unique: null
}
9 => Faker\Provider\Lorem {#573
#generator: Faker\Generator {#560}
#unique: null
}
10 => Faker\Provider\Internet {#572
#generator: Faker\Generator {#560}
#unique: null
}
11 => Faker\Provider\Image {#571
#generator: Faker\Generator {#560}
#unique: null
}
12 => Faker\Provider\File {#565
#generator: Faker\Generator {#560}
#unique: null
}
13 => Faker\Provider\DateTime {#562
#generator: Faker\Generator {#560}
#unique: null
}
14 => Faker\Provider\en_US\Company {#567
#generator: Faker\Generator {#560}
#unique: null
}
15 => Faker\Provider\Color {#566
#generator: Faker\Generator {#560}
#unique: null
}
16 => Faker\Provider\Biased {#568
#generator: Faker\Generator {#560}
#unique: null
}
17 => Faker\Provider\Barcode {#569
#generator: Faker\Generator {#560}
#unique: null
}
18 => Faker\Provider\en_US\Address {#570
#generator: Faker\Generator {#560}
#unique: null
}
]
#formatters: []
}

To be honest, Faker creates fake Dutch Person that have hilarious and not so common names:

  1. Leon Gansneb genaamd Tengnagel tot Bonkenhave Msc
  2. Dubbeldemuts van der Sluys
  3. Thijmen Elsjan de Wipper
  4. drs Pien Gellemeyer
  5. Paspoort van Grijpskerke en Poppendamme

My god, those boys and girls most have had a hard time on school, ruthlessly ragged on those weird names.

Thank goodness it’s Laravel in a test-environment.

 

Connecting to your Homestead Laravel 5.1 VM with your trusty phpMyAdmin

Monday, June 15th, 2015

Suppose your developing with the php Laravel 5.1 framework for Nginx, and for that your using a vagrant VirtualBox virtual machine for Nginx.

Homestead/Vagrant is easy. You can follow this great set-up.

Laravel 5 uses database migration and has support for database factories. But still sometimes you wanna use your trusty phpMyAdmin for database management on the VM. Of course you can take the time and install phpMyAdmin on your new virtual machine, but I’m gonna give you a faster tip.

Why don’t you use your phpMyAdmin on your local server to manage the database on the VM.

For that edit the phpMyAdmin config file

vi /etc/phpmyadmin/config.inc.php

and add this after $i++

$i++
  $cfg['Servers'][$i]['host'] = '127.0.0.1'; //provide hostname and port if other than default
    $cfg['Servers'][$i]['port'] = '33060';      //user name for your remote server
    $cfg['Servers'][$i]['user'] = 'homestead';  //user name for your remote server
    $cfg['Servers'][$i]['password'] = 'secret';  //password
    $cfg['Servers'][$i]['auth_type'] = 'config';       // keep it as config

Now you can select the VM database in phpMyAdmin in the current server select field.

Neat.

CSS vendor prefix macro for Netbeans

Wednesday, February 15th, 2012

There is all about CSS vendor prefixes at the moment. PPK doesn’t like them at all, the W3C is afraid that webkit might rule the world, developers seem to find that webkit is the only mobile browser around.

Agreed, the CSS vendor prefixes make verbose code. It’s ugly, and sometimes it’s a lot of typing. But they pinpoint a real problem, browsers need them, and although maybe obsolete in the future, if you want your code cross browser now and future-proof you simply have to use them. No matter if they’re going to be abolished or not. No matter what solutions is to be found tomorrow.

But what most people fail to notice, that it’s actually an IDE/deployment problem. If IDE can spellcheck and code-complete why can’t they extend CSS prefixed rules.

Well they can. So we created a macro for Netbeans. Netbeans is our favourite PHP-IDE.  The macro recorder in Netbeans is easy and fast, so it suits.

Just add  a Macro in Netbeans: tools -> options ->  editor -> macros -> new.

Paste this


copy-selection-else-line-down copy-selection-else-line-down copy-selection-else-line-down copy-selection-else-line-down copy-selection-else-line-down caret-up caret-up caret-up caret-up caret-begin-line caret-forward delete-next "moz" caret-down caret-begin-line caret-forward selection-forward "ms" caret-down caret-backward selection-backward "webkit" caret-down caret-backward caret-backward caret-backward caret-backward caret-backward selection-backward "khtml" caret-down caret-backward caret-backward caret-backward delete-previous delete-previous delete-previous

And give it a proper shortcut CTRL + ] is free.

Then start writing your code for the least popular browser, but it has the shortest prefix vendor: Opera.


-o-transition: opacity 1s ease-in 1s;

Put your cursor soemwhere in the line and press CTRL + ]

You get this result


-o-transition: opacity 1s ease-in 1s;
 -moz-transition: opacity 1s ease-in 1s;
 -ms-transition: opacity 1s ease-in 1s;
 -webkit-transition: opacity 1s ease-in 1s;
 -khtml-transition: opacity 1s ease-in 1s;
 transition: opacity 1s ease-in 1s;

Probably khtml can be skipped, I don’t know the market share, but let’s try to support everything.

Has become hell a heaven. No but it isn’t that bad any more. Are they’re caveats. Sure, like different implementations, but that should be corrected by hand.

Important thing is, the rule without a prefix should end the list.

Your are browsing
the Archives of My Beloved PHP in the 'Tutorial' Category.
Categories
Archives
Links