Laravel Macros, frontend presets, and Jigsaw
April 25, 2017

"Laravel Macros, frontend presets, and Jigsaw" Full Transcript

This is the full transcript of the Laravel News podcast episode #36 - “Laravel Macros, frontend presets, and Jigsaw


Jacob Bennett: Hello, welcome back to the Laravel [00:00:30] News Podcast. We have a couple news items to talk about today, but before we do, wanted to take just a couple of minutes to talk about what we're hoping that the next couple of weeks, or actually, I suppose over the next couple of episodes of the Laravel News Podcast leading up to Laracon are going to look like.

Michael, do you just want to take a quick to explain to our listeners what we're hoping to do in the coming weeks?

Michael Dyrynda: Yes. What you and I and Eric had spoken about probably a little while ago, and we're now kind of late to the party, but we're putting a schedule together [00:01:00] is that we're going to start interviewing a number of different, I think probably five or six speakers that we'll get to before Laracon U.S. this year. What we want to do is sort of go throughout I guess our standard interview format with them, find out a bit about them, get them to introduce themselves, talk a bit about what they do, what they're talk is going to be about. Then what we're hoping to do is to put together a little playlist which we share on Spotify with probably two or three songs from each speaker, just so you can get a bit [00:01:30] of a playlist for yourself as you make the journey, for those of you who are going to Laracon, so something to list to and just a little bit of engagement.

The thing that I'm most looking forward about this, aside from obviously chatting to all those speakers, is that the last scheduled episode that we have, I'm actually going to be in Chicago at the time.

Jacob Bennett: Yes, which is only about two hours from me.

Michael Dyrynda: Yes, so I'm really hoping that we can make it work that Jake and I actually get to do [00:02:00] an episode sitting face to face. We'll probably try and get a couple of speakers on and just do them back to back. Fingers crossed that all pans out, but we'll have a schedule together. Did you, Jake, I know you love saying his name and you've learned now. Did you want to let us know who our first speaker is going to be?

Jacob Bennett: Okay. I was given a tip on this. I was told to say it like Drake, the singer, but with an 'f'. [00:02:30] Frake.

Michael Dyrynda: There we go. He's got it.

Jacob Bennett: Yes, even roll the 'r'.

Michael Dyrynda: You rolled the 'r' like a boss.

Jacob Bennett: We were going to have Freek on today. That what we were hoping to do. He's got some other things that he was tied up with. We couldn't make it work for this episode. We're hoping to have him on next episode. The thing is really at these conferences and these talks, they have enough time to get up on the stage and give their talk of maybe a quick intro. You don't really get to know a lot about the people unless you do some research on your own or unless [00:03:00] you're already following them or whatever. I like this format just because we'll be able to get to know the speakers a little bit before we get there. You get to hear a little bit of their background and then get to hear their talks. It should be great. It should be great.

Michael Dyrynda: Yeah, looking forward to it.

Jacob Bennett: What we had, since last time we were on, we've had a couple of point releases for Laravel 5.4.18 is now released.

Okay, everyone, Michael and I have actually spent the last five minutes tearing this apart. We've been looking that change log here and we're trying to figure out what this [00:03:30] nested resource model controllers thing is that made it into the 5.4.18 release. We think we've got it figured out. We're going to try and share it with you here.

Let's say the situation is that you have a nested resource. For example, you have a client and you also have a document. These are both their own models. You have a client model; you have a document model. You also have a nested resource, which would be a document that belongs [00:04:00] to a client. Your structure might look something like myapp.com/client/. Then you'd have your client i.d. and then /document and then /your document id. This is sort of one way that you could structure your URLs if you're trying to access a nested resource. What that would do is that would grab client one maybe and document one, so the document that belongs to that client.

What this pretty much allows [00:04:30] you to do is to quickly make a resourceful controller from your command line using these two models. You would say PHP artisan make control. Then the name of your controller. In this case maybe client document controller. Then you can set the resource that you would be accessing, and that would be parent. That would be the parent, you know, the first one of those resources I suppose you would call it. In this case, client would be that parent [00:05:00] and document would be that resource.

Really, the thing that Michael and I were struggling with was we were trying to figure out what exactly it is that this command is doing, if it was just generating a control for you, or what it does. I think what we've discovered is that this will handle the implicit route model binding portion for you. In your controller method, you can pass in type hinted model for your client and a type hinted model for your document. If you structure your [00:05:30] URLs as was just mentioned previously, it will auto-load in those resources by the time you're into your controller method. It will auto-resolve them. It will look up the model by whatever set on its key.

Michael, what's that attribute that it looks up a model by?

Michael Dyrynda: Yeah, The Get Key Name. Well, Get Route Key Name. Sorry.

Jacob Bennett: Get Route Key Name. It will try and do a find or fail based on what that route key name is on your model. Then it will return [00:06:00] those to your controller for you, all nice and tidy without you having to do any look ups inside your controller method themselves.

I'm not sure that I'll every use it. It might be something that I would rather do myself. I might be more comfortable doing it myself. If not, if you've not used nested resources before and you want to just play around with it, see how you might structure it if you were following strictly by the book, this might be something you'd want to check out.

Michael Dyrynda: Yeah, sure.

Jacob Bennett: Sorry about that long rambling explanation there. [00:06:30] We've actually just been trying to figure it out here. That's the best we got.

Michael Dyrynda: There is, of course, a blog post on Laravel News, which you can refer to, if none of that made any sense to you.

Jacob Bennett: ... which it may not have. In addition to 5.4.18, we also had a 5.4.19 release come out on the 16th. Really the highlights for that are that we just have a couple new macroable classes. Mailer is now macroable and the session guard is now macroable. There's a couple of other smaller changes that you could [00:07:00] check out. Those are the ones that I'd be most interested in.

Michael Dyrynda: Yeah, and for those of you who don't know what the macroable means, it basically is an implementation on any given class that allows you to specify, usually with an app service provider somewhere, the ability to define methods on the fly in your application. If you wanted to extend, say, the mailer in some way to add a new method to it, but you didn't feel that it was something that needed to be [00:07:30] added to the framework itself, or if it was just a once off thing, you can basically define this in your service provide. You would go mailer::macro and then you would pass it the name of the function that you'd like to call, and then you'd pass the second argument would be a closure that would do whatever it is that you need it to do. That way you can call directly on the mailer in your method without having to dig in and try and override the framework methods.

Jacob Bennett: Absolutely. [00:08:00] There's a number of these classes that are macroable now. I believe the first one that was macroable was either the request class or the collection class, one of those two. I know that both of those, those two right there, I think, are probably the most popular, ones that actually get macro-ed. Actually, or is it, there's also ... Is the database macroable?

Michael Dyrynda: Eloquent is not, no. I don't know if the Query Builder is.

Jacob Bennett: That's what I was trying to figure out, if Query Builder was. I think there's something with that. I think there's something with that. I'll have to look that up and get back to you.

Request and collections [00:08:30] are macroable and there's actually Frake, our guest for next week, has a whole bunch of macros for the collection class which you can pull in through a package. There's a lot of really handy ones in that that might not be, you know, they're not, obviously, included in the default Laravel stuff. They're pretty handy methods there. You might want to check those out. We can link those up in the show notes.

Michael Dyrynda: You are correct. The Query Builder is macroable.

Jacob Bennett: Okay. Perfect. Another that we have is that Laravel 5.5 is going [00:09:00] to have front-end presets. Since Laravel 5.3, the framework has shipped with, we'll say optional bootstrap and Vue.js scaffolding. It comes out of the box like that. We can say optional, but really it comes with them. It is set up and then you can remove them if you'd like. We have, you know, Laravel being an opinionated framework, which is totally fine, has decided that Vue is way that they wanted to go. Well, there's a few people in the community that really enjoy using React. Samantha Geitz... Is [00:09:30] it [Gites 00:09:30] or [Geets 00:09:30]?

Michael Dyrynda: Geitz.

Jacob Bennett: Gites. Samantha Geitz created a React preset. You can say PHP artisan preset React. Then that will replace the Vue.js scaffolding with React scaffolding, which is pretty neat. Also, you can remove Vue altogether and just keep bootstrap if you'd like. You have PHP artisan preset bootstrap. Of course, you can remove all of the scaffolding if you'd like which is PHP artisan preset none. This really gives you a couple [00:10:00] options for getting started with your Laravel application. You can start from scratch with no opinions as far as front-end stuff is concerned and just remove it all, or you can just stick with Bootstrap or you can go with React or you can go with Vue and Bootstrap. It's really nice that people are getting some options for where they start. I think that will be good for our community.

Michael Dyrynda: Absolutely, yeah. It makes it easy for those who don't want to use Vue or who are familiar, as you say, with React, or they don't want to use Bootstrap. They just want to roll with something from [00:10:30] scratch. It will be interesting to see what happens with it moving forward with a ... I'm not really expect Taylor to maintain something for semantic UI and something for Bulma, and so on and so forth because there's so many different US CSS frameworks and job descrip frameworks. It'll be cool to see if it will become extendable in some way later on, so people can easily hook into it and add their own at some point.

Jacob Bennett: I almost think it might. That would be interesting. Almost like a macroable front-end preset where you can [00:11:00] have a list of different types of presets that you would have out there and the community contributions. You can start with Bulma or you can start with Beard CSS or you can start with some of those different CSS frameworks. I suppose it's not really that difficult to get started with those yourself, but if you just wanted to always have that preset you could start your projects with, it would be pretty cool if that did become something that you could add different things onto.

Michael Dyrynda: Yeah, cool.

Jacob Bennett: One of the things I'm really excited about it 5.5 that has just been announced [00:11:30] as going to be released is custom exception reporting. Michael, do you want to tell us about how that works.

Michael Dyrynda: Yeah, so Jake and I were talking a little bit about this before we started the show. There was a blog post that was put out a little while ago by ... We both forgot who it was, but [crosstalk 00:11:49].

Jacob Bennett: By Ross Tuck.

Michael Dyrynda: Yeah, so Ross put out this blog post back in 2015, which talks about formatting exception messages where you would have static [00:12:00] methods inside your exception classes that you would pass in some relevant information. Then you could really construct a useful explicit message in a formatted way that makes sense to present wherever you're going to be presenting those exception classes. That was one way of handling it. I think Freek actually picked up on that and then started talking about it a little bit more last year.

In Laravel 5.5, it's going to be possible [00:12:30] to define a report method on any of these exceptions that you define yourself. Laravel will then look for the existence of this method on any class or on any exception that's thrown. If it exists, it will return the result of that report method rather than bailing out to the default exception handler. Its kind of convenient if you wanted to use that as a way to email a developer or to report to, if you're using Bug Snag or anything like that, [00:13:00] or if you want it to carry out any other action when that exception is thrown.

In current, I guess, we will say previous for the perspective of 5.5, but in any current version of Laravel, it is possible to achieve the same thing by editing the report method in your apps exception handler clause. You can look for specific things. As you can imagine, it gets tedious as the application curves and you add more exceptions and things like that. The one thing that I had bumped on when I was [00:13:30] speaking with Jake about it earlier, was that with, and we'll link this up on the show notes, Ross's post, the way that it was done was that you would have a generic exception for a behavior or I guess a section of your application. Then you would have well-defined names of methods within that. It read very nicely when you were going through the code base.

If you're going to use the report, you would probably then have one exception for each [00:14:00] instead of one exception that would have maybe two or three different methods inside it. I guess it doesn't really matter. It's the same argument as having lots of controllers in your application.

Jacob Bennett: That is true.

Michael Dyrynda: Yeah.

Jacob Bennett: Yeah, and I feel like when this is most useful is when you do need to perform some action on your end that would be outside of what you would do for a normal exception. Really, instead of having to do a bunch of 'if' checks or a switch in your handler, .php [00:14:30] class in your Laravel application, it gives you a little more granularity by being able to define that on the exception itself, what it should do for the report. It's not just returning the message, right. It's actually, instead of calling the report method on the handler .PHP class, it will run this instead of that.

If, for instance, you wanted to normally report to Bug Snag or Century or whatever, but for a specific type of message, it's not necessarily worthy of going to one of those, but you want to know about [00:15:00] it. You need a Slack message or something like that. For instance, I have an app that requires that a person when they sign up, have a manager in our building. I don't want the application to crash and burn and fail. I can still continue if they don't, but I need to know about it pretty quickly. In those instances, I would want to give myself a Slack message and say, hey, just heads up. You need to got add this manager in the database table and then should be all set. [00:15:30] As long as I know about it, that's fine. I don't really want to thrown into Century. It's not necessarily an error. It's just something that I need to handle on my end.

This would be a good case where I could use something like that. I make a custom exception, make a report method on that exception, and then I won't have to check for it in my report method on my handler class.

Michael Dyrynda: Yep.

Jacob Bennett: Pretty useful stuff.

Michael Dyrynda: Yep. Definitely.

Jacob Bennett: Awesome.

Michael Dyrynda: Jake, we all know how much you love PHP and so on.

Jacob Bennett: I do, I do.

Michael Dyrynda: We all know [00:16:00] how much you like Adam Wathan's Sublime Text Helpers for Running Unit Tests. There is a post. I think that maybe even you discovered it. It's now found it's way onto the Laravel News site, which talks about doing a very similar thing running PHPUnit in PhpStorm. Do you want to tell us a bit about that?

Jacob Bennett: Yes, I absolutely do want to talk about this. There's a guy out there. His name is Jonathan Williamson. He emailed me after we had had a recent show and talked about exactly what you said. Adam Wathan has this way to [00:16:30] run tests really quickly using Sublime. I wanted to be able to do the same in PhpStorm. I was under the impression that the only way that you can do that was to actually go over to the test and right click it and run the test from there. You can run it from PhpStorm, but I was not aware that you could assign it to a keyboard shortcut. Well, you can.

You hooked us up. Your PHPUnit in your PhpStorm, which is not super complex, but there's a couple of things [00:17:00] you have to do with that. Once you have that set up, then you can assign it to a keyboard shortcut. You can run the entire class of your tests, or you can just run a single method in that test class. Depending on where your cursor is, if your cursor is outside of one of the tests, then it will run all of them when you press your keyboard shortcut. If it is in the ... I believe it's if it's in the method name, if your cursor is on or in the string of the method name, and you press the shortcut, it will run just [00:17:30] the test for that single method.

I've used it a couple times. I don't have the keyboard shortcut yet set up to do it straight from there. I've just been, I got PHPUnit set up in my PhpStorm this last week. I've used it manually a couple times. I've got to do the key binding. Then I'm going to be a happy camper.

Yeah, pretty cool. There's a little video out there that Jonathan made to walk you through getting set up with that. I think it's like a, yeah, it's a one and a half minute video. [00:18:00] It's pretty easy to set up and definitely useful. There you go, all of you people who hate PHP Storm and are always telling me how much better Sublime is. We've got another equalizer here.

Michael Dyrynda: All right. That's enough out of you.

Jacob Bennett: You know, consequently, Freek is also a PhpStorm user.

Michael Dyrynda: I'm going to have to start finding Sublime text users to come on.

Jacob Bennett: We are going to have two on one. Yeah, we're going to have two on one next week or next episode.

Michael Dyrynda: [00:18:30] Use whichever editor makes you happy.

Jacob Bennett: That's right. Absolutely. I mean, some people use, a Visual Studio is a really popular one right now too. Is that what it's called? Visual Studio? [crosstalk 00:18:42]

Michael Dyrynda: VSC.

Jacob Bennett: Yeah. A lot of JavaScript developers have been jumping on that, I know. I've seem some of the folks in our community trying it out as well. Man, there's a lot of great editors out there. Thank goodness. It used to be Notepad++ was the only one around. It was the only way you can [00:19:00] get any sort of highlighting or anything. Now we've got a million of them. Definitely thankful for that.

A couple of weeks ago, we talked about F-Bar and we had talked about the release of the Forge API. F-Bar is built on top of that. It uses your API key to interact with your Forge account and then allows you to see your servers and upload SHH keys and directly SHH into the box all from your MAC toolbar. Another tool that is very [00:19:30] popular, or has been really popular in the past, is Alfred. I know there's a lot of people who still use it, who absolutely love it. I am not an Alfred user, but I may have to try it out again after we talk about this.

You can now manage your Forge servers straight from Alfred. There is a number of workflows that have been created that will allow you to very quickly do a couple commands. Some of those commands are Forge open, so you can open the specified server [00:20:00] or sites Forge page right in your browser. I actually use something like this for [inaudible 00:20:04] from my command line where I say like[inaudible 00:20:07] repo. It will open the page of my repo that I'm in on the command line. This you just say Forge open and then you type the name of the site that you're looking to get to the page for or the server and it will intelligently figure out which one it is. It will take you to the page that you can manage that on straight in Forge. You can trigger a deploy. You can get the contents of an ENV. You can get the IP of a server.

You [00:20:30] can reboot. You can get to MYSQL. Most of the things you can do through the API, you can do through one of these Alfred workflows.

There's a whole bunch of them. We'll link those up in the show notes, so those of you who are Alfred users or those of you who have been looking for an excuse to get on Alfred can try that out. I am wondering, however, are workflows only enabled for Power users or for people who have purchased Alfred because Alfred is a free product.

Michael Dyrynda: Yeah, that is a good [00:21:00] question. I know that the workflows themselves are free.

Jacob Bennett: Yeah, so you do have to have purchased the Alfred app in order to get these. I suppose this is probably geared more towards people who are already using Alfred. Hey, if you want to but the power pack and try it out, of course you can do that as well. We're all about supporting software developers on here, so good for you.

Have you ever found yourself in that spot where you love using Laravel but you're now at the point where you really just need a static site [00:21:30] and you don't really want all the weight of Laravel sitting behind ... I say all the weight ... You don't want the weight of Laravel sitting behind your static site. Well, a couple, I think it was a year ago, or maybe I think maybe a couple of years ago. I'm trying to remember. Adam Wathan had made a static site generator using Blade that is called Jigsaw. It has been out for a while and actually Adam created it. Since then, Titan has picked up the maintenance and the development of that. They just tagged Version 1.0. Michael, do [00:22:00] you want to tell us a little bit about the features that they rolled out in 1.0 and how we can use them?

Michael Dyrynda: Yeah, so it was a pretty nice static site builder before. The developer went quiet on it, which if you're not using it all the time, or if you're not actively participating, that happens from time to time, especially if you're doing it as a side piece. Now that they've tagged 1.0, they'd added the addition of collections. [00:22:30] Now, these collections are not the same kind of collections that you think of in terms of the package that's included as part of Laravel. What collections are in the context of Jigsaw is allowing you to work with lists of related content, like blog posts or portfolios or biographies of staff and things like that.

This version, obviously, still is running on Blade. You can keep using that same syntax that you're familiar from any of your Laravel applications.

[00:23:00] It is also paired with markdown content. That makes it really easy to get new content generated and then published onto your site. It's basically been designed to be as simple as possible to use and implement. It gives you a lot of power and a lot of flexibility in creating some reasonable advanced sites. There is some more information and some examples on the website. Obviously, we're not going to, we've said this before. We're not going to read code out on the [00:23:30] air. It doesn't help anybody. We will certainly link it up in the show notes. There is some, the addition of pagination as well in this new release. Also, some really creative ways of implementing variables and helpers via sort of config and the [inaudible 00:23:49] front matter that's included in your blog post. As I said, I won't go into the code itself orally, but I will certainly make sure [00:24:00] that the blog post is in the show notes for you to check out. I encourage you to check out Jigsaw if you are interested in doing any blogging.

Jacob Bennett: Yeah, for sure. I don't have anything to add to that. That's good. It does utilize Gulp and Elixir behind the scenes as well, which is nice because a lot of us are already familiar with that as well. You don't have to learn or set up another build tool for a different static site [00:24:30] generator that you might be using. Basically if Laravel is what you're really comfortable with, this is a great transition for you to a little static site generator because you're very familiar with Blade, which it uses. You're familiar with Gulp on Elixir, which it also uses. It's also got a Jigsaw console command, which is similar to what we have with PHP Artisan. Definitely worth checking out if you're looking for something to use for static sites. It's also maintained by one of the best Laravel shops around, Titan Co. They're not going anywhere. They're going to be [00:25:00] around to maintain this thing. You know you've got future support for future versions. If anything breaks, you can know that you can count on them to fix that stuff. There we go.

Michael Dyrynda: There we go.

Jacob Bennett: Excellent. Well, thank you everyone so much for listening today. If you liked this episode, please feel free to rate it up five stars in iTunes or your pod catcher of choice. Show notes for this episode will be located at Laravel/news.com/podcast/36. If you have any questions for future episodes or would like [00:25:30] to reach out to Michael or myself, feel free to hit us up on our Twitter accounts or at Laravel News on Twitter. Join us in a couple of weeks to take with Freek about all the things that he's working on, what he's going to be talking a little bit about at Laracon, and get to know him a little bit better if you don't already know him.

Michael Dyrynda: Awesome.

Jacob Bennett: Awesome. All right, mate.

Michael Dyrynda: All right.

Jacob Bennett: Let's wrap this one up.

Michael Dyrynda: Wrap it, wrap it in paper. Stick a bow on it. We are done. [00:26:00] All right. I'm going to try and come up with a different goodbye, a different language for every episode.

Jacob Bennett: Yeah, that's a great idea.

Michael Dyrynda: Because I haven't planned this further ahead than just then, I'm going to stick with what I know, and that it do widzenia. That is Polish my friend.

Jacob Bennett: Do widzenia. Do widzenia is goodbye in Polish.

Michael Dyrynda: Do widzenia, until next time.

Jacob Bennett: Very nice. I like that. It sounds [00:26:30] good. I'll let you go, my friend.

Michael Dyrynda: Cheers. Have a good sleep.

Jacob Bennett: All right. Take it easy, bud.

Michael Dyrynda: Bye.

Jacob Bennett: Thanks, bud. Bye.

Laravel News Partners

Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.