LouWii's Blog

< Web developer & geek stuff />

Tag: php

Anatomy of a good ExpressionEngine add-on

ExpressionEngine 3 has brought a lot of changes and if you’re coming from EE2, you might miss some that are really interesting. Ellislab team has a done a good job of bringing APIs and services for third party developers to use in their add-ons. Your code will be cleaner than ever 🙂

Here is just a selection of things that are used in almost every add-ons, the ones that you should really use in your next project.

Use Models

Documentation

Messing around with SQL queries might be ok, but if you add-on is quite complex and modify EE data, you might want to switch to using models. Models help you handling EE data (channel, members…) but you can also create your own ones. You’ll just rely on EE code and APIs, it’s way safer and will facilitate maintaining your code.

Plus it totally makes sense to use that service when doing OOP. It’s way easier to get back Objects and play with them, than getting back SQL results and process them before doing anything relevant.

Use shared form view

Documentation

Creating forms can be long and complex. But with the shared form view, it’s so easy you’ll even enjoy it. You basically need to declare your form using arrays. A lot of different field types are available. EE will generate all the HTML for you. Honestly, dealing with add-on settings is no more a pain now.

Use validation service

Documentation

Now it’s time to validate the user inputs. Again, no more pain, just use the validation service. It contains pre-defined rules and you can also create custom ones. Easy to write, easy to maintain, and a nice beautiful code. 😀

Add internal documentation

Documentation

This is not a code tip, but EE now includes internal doc. If you add a README.md file inside your add-on folder, you’ll be able to access that file from within EE. It might not be useful to you, but it’ll surely be for other developers out there that will need your add-on doc. 😉

Check the other services

ExpressionEngine 3 is providing other services that you might want to use, check them out.

EE3 architecture is working with dependencies, prefixes and providers.

There’s even a tree data structure if you ever need one.

Handling several PHP versions on OSX (including php7)

I was trying to use Phan, a PHP code analyser, on my Macbook, but it’s requiring PHP 7 cli. I never modified the PHP cli on my machine, mainly because I use MAMP to handle different environments. But here I had no choice. Here is the way I handled it.

Easy trick : use aliases

Yeah, it’s way too easy that way, just create an alias from your command line pointing to a php bin. In my case, I made it point to php7 bin from MAMP.

Note : you can name it whatever you want. php7 could be a good option too. 😉

To remove the alias :

To make the changes effective, either close and open your terminal again, or type in

Cleanest way : using brew

If you have Homebrew installed on your machine, it’s pretty easy to install whatever version of PHP with it. And it’ll take care of every cli bins.

In my case, I had a 5.5 version of PHP already installed, so I need to tell brew to unlink it before installing another one

And then, let brew do the dirty work for you

Et voilà !

 

Lazy way to make your WordPress content working with HTTPS

Yeah, we’re all lazy sometime. I recently added HTTPS support to this blog but the content saved might contain http links or image addresses. Instead of updating the whole database, you can just add this to your theme functions.php file :

Problem solved ! I know, it’s not very clean, but it’s working great.

© 2018 LouWii's Blog

Theme by Anders NorenUp ↑