CraftQuest Articles

Tutorials, tips, and thoughts on Craft CMS, modern web development, and more.

Image

11 Things You Need to Know About Craft 3

Posted on Dec 03, 2017 by Ryan Irelan

Reading time: 2 minutes, 7 seconds

There are dozens of changes to Craft in ver­sion 3 but below I’ve com­piled the eleven things you need to know to have a smooth tran­si­tion from Craft 2 to Craft 3.

Ready? Let’s go.

1. Instal­lable via Com­pos­er #

If you installed Craft 3 dur­ing the beta peri­od then you will know that Craft can now be installed with Com­pos­er. A nui­sance to some but a big step up for oth­ers, Com­pos­er will make it easy to man­age depen­den­cies in Craft.

After you have Com­pos­er installed, you are ready to install Craft 3

2. Uses Twig 2 #

Craft 3 uses Twig 2. This update to Twig is, for the most part, a clean-up release. Here are a few impor­tant things to know about Twig 2 with regards to Craft:

  • Twig 2 Requires PHP 7 (and so does Craft 3)
  • Your cur­rent tem­plates should work as-is with Twig 2. There is some dep­re­cat­ed tem­plate code but that’s relat­ed to Craft ser­vices, not Twig.
  • Twig slight­ly changed how macros are han­dled. All macros defined in-tem­plate must be import­ed explic­it­ly in each tem­plate in which you want to use them (includ­ing includ­ed” tem­plates). See the docs for more infor­ma­tion on macros.

3. Access to Craft ser­vice APIs in your tem­plates #

As of Craft 3 you now have access to the Craft ser­vice APIs right inside of your tem­plates. Pre­vi­ous­ly, Craft made avail­able some spe­cial tem­plate func­tions that made some of the ser­vice APIs available. 

Many of those tem­plate func­tions are going away in Craft 3 in favor of using the new ser­vice API access.

These tem­plate func­tions were most­ly just Twig vari­ables that exposed the data via an exist­ing func­tion. So instead of doing that we just get direct access to the func­tion itself.

There is no PHP allowed in Craft tem­plates, so pre­vi­ous­ly if you want­ed to access some­thing in the back­end of Craft you had to use a plugin. 

Using a plu­g­in is still a good idea if you have a sig­nif­i­cant amount of fea­tures you need to achieve, but for sim­ple one-off things, hav­ing access to the Ser­vices inside of your tem­plate is going to be a game changer.

4. Mul­ti-site Sup­port #

Anoth­er new fea­ture in Craft 3 is mul­ti-site. This was a pop­u­lar fea­ture when it was first announced but there was some con­fu­sion about it so let’s clear things up.

The Craft 3 mul­ti-site fea­ture allows you to han­dle mul­ti­ple sites of con­tent right from the Craft con­trol pan­el. This could be a mul­ti-lan­guage site, where there’s a dif­fer­ent ver­sion of the site for dif­fer­ent lan­guages, or mul­ti­ple sites with sim­i­lar con­tent (like micro sites for prod­uct lines).

Note: this fea­ture is an enhance­ment of and replaces Locales in Craft 2.

5. Bet­ter Debug­ging #

The Yii Debug Tool­bar is a new fea­ture in Craft 3 that allows you to have a con­stant tool­bar at the bot­tom of your front-end and con­trol pan­el pages. 

This tool­bar gives you access to impor­tant debug­ging infor­ma­tion includ­ing items that aren’t spe­cif­ic to Craft.

6. Com­mand Line Inter­face #

When you are done installing Craft with Com­pos­er, you’ll notice a lit­tle mes­sage at the end, just before the new com­mand prompt, encour­ag­ing you to use the Craft set­up command.

Craft 3 offers a new way to set up the site, which replaces the nor­mal set up you do via the web browser.

So, why is this important?

  • Keeps you right in the ter­mi­nal after installing
  • Pos­si­ble to script the set­up process — maybe part of a big­ger rou­tine where you’re installing and set­ting up a new installation.
  • It auto­mates the pop­u­lat­ing of your .env file for the site’s data­base connection.

The craft CLI offers sev­er­al options, but we’re only going to con­cern our­selves right now with setup command.

7. Hash Redi­rect Para­me­ters #

Back in ver­sion 2.5, Pix­el & Ton­ic intro­duced a hash fil­ter and lat­er in a 2.6 release the abil­i­ty to require hash­ing of the redi­rect para­me­ters that are set in forms (like when you sign in or sign up).

They intro­duced this enhance­ment to make Craft more secure. There is a poten­tial Denial of Ser­vice secu­ri­ty issue with in -the-clear form data. In the exam­ple of the redi­rect para­me­ter, this could allow some­one to redi­rect the user to some­thing oth­er than what is spec­i­fied in the hid­den form element.

In Craft 3, how­ev­er, the hash­ing of redi­rect para­me­ters is required.

8. Always use .all() #

In Craft 2, we might do some­thing like:

{% for entry in craft.entries.section('news') %}
   {{ entry.title }}
{% endfor %}

We iter­ate over the ele­ment query direct­ly. Behind the scenes Craft knew we want­ed that actu­al data so it calls find() for us automatically.

Based on a prob­lem dis­cov­ered with Twig’s loop vari­ables and Craft, the Craft team decid­ed to just force the usage of .all() and dep­re­cate the usage of iter­at­ing over the ele­ment query with­out explic­it­ly call­ing for the results.

What does this mean? 

We need to now use all() on every ele­ment query in Craft 3. If you don’t it will still work but you’ll receive a dep­re­ca­tion error in your log (and it’ll break in Craft 4).

So the above code is now: 

{% for entry in craft.entries.section('news').all() %}
   {{ entry.title }}
{% endfor %}

9. Use one() instead of first() #

In Craft 3 the first() method to exe­cute an Ele­ment Query is now dep­re­cat­ed in favor of one(). The new method will return the same thing as first() (the first match­ing ele­ment) and also return null if there are no matches.

So, this code:

{% set entry = craft.entries.section('news').first() %}
  {{ entry.title }}

Will now need to be writ­ten as:

{% set entry = craft.entries.section('news').one() %}
  {{ entry.title }}

10. Sup­port for Post­greSQL #

You hatin’ on MySQL or maybe your sysad­min thinks Post­greSQL is bet­ter? Now you can run Craft off a Post­greSQL data­base server. 

11. Remote Assets are Now Plu­g­ins #

Remote Assets Vol­umes (they’re called Vol­umes in Craft 3) now require a free, third-par­ty plu­g­in. The default instal­la­tion of Craft only includes Local Vol­ume sup­port. All three plu­g­ins are avail­able on the Craft Plu­g­in Store.

Is that it? #

Well, that’s 11. But if you want even more here’s a big list of changes from Craft 2 to Craft 3.

© 2018 Mijingo, LLC