Using the str helper0:00
All right, next up, let's take a look at two new helper functions in Laravel 9 that you should be aware of. So, for example, when I load the home page, we have this new str function. Now, this is just a helper to quickly generate a Stringable object. Yeah, it's mostly an alias for, or it's entirely an alias for, the stringOf method. And if you're not familiar with that, let's switch back. stringOf, you provide any string, and it gives you an object-oriented way to manipulate the string versus sometimes using those confusing string helper functions that php provides. So, for example, if I want to make everything uppercase, then I can call the upper method. View it in the browser, and, of course, we get Hello World, all in caps. Or I could say, what else do we have here? How about I want to append something to the end? All right. All right, come on back, give it a refresh, and you see how this works. Incredibly useful.
Or I could say, what else do we have here? How about I want to append something to the end? All right. All right, come on back, give it a refresh, and you see how this works. Incredibly useful. I reach for this all the time. Okay, yeah, in Laravel 9, if you want, just a slight tweak, but you can now use a str function. And if nothing else, this allows you to avoid the string import in your class or potentially using the fully qualified class name within a view. So, yeah, you'll see I can do the exact same thing here. Come back, refresh, and, yeah, it's doing the same thing because it is the same thing. It's just a small convenience. But, yeah, real quick, if we have a look at the implementation, you can see if we call the string function and we don't give it an argument, then it sets up an anonymous class again, and it uses magic methods to basically forward any method calls. And actually, that might be a
Comparing Stringable vs String1:37
function and we don't give it an argument, then it sets up an anonymous class again, and it uses magic methods to basically forward any method calls. And actually, that might be a little confusing because the standard string class and what you get with string of are different. string of returns a stringable object, which, again, is an object-oriented way to operate upon a string, whereas the standard string class can do any number of things. So, for example, slug, you give it some kind of title and then it will generate a slug based on that. But notice with this, it's effectively a static method. There's no underlying value for the object. So you can use both with the string helper. So, for example, the equivalent would be string of, which creates the stringable class, and then you call a slug method. So, real quick, we have a look in Firefox and you get a slugified version of that string. The kind of the old school
Redirecting with to_route2:26
which creates the Stringable class, and then you call a slug method. So, real quick, we have a look in Firefox and you get a slugified version of that string. The kind of the old school equivalent would be string, and then we call slug and we give it hello world. And yeah, if we switch back and give it a refresh, we get the exact same thing. So yeah, just a slight little thing to be aware of there. Okay, next is a simple way to redirect. So, for example, if I set up some kind of endpoint here, it doesn't matter what it does because it's going to instantly redirect us back to the homepage. All right, and we will uncomment that. All right, so let's see what happens here. We'll hit that endpoint, and of course, it instantly redirects us back to this homepage. Okay, another thing we could do is give the homepage a name of home, and then I could say redirect not to a hard-coded URI, but whatever the route for home is. And that
us back to this homepage. Okay, another thing we could do is give the homepage a name of home, and then I could say redirect not to a hard-coded URI, but whatever the route for home is. And that way, if in the future your home route changes, this would not need to be updated. So yeah, once again, nothing new here as part of Laravel 9. It still works. But what is new is a helper function to simplify this a little bit. We could instead say return to the route called home. So yeah, this and this are functionally identical. Have a look at to route. It's the same thing. It's just a small little helper that you might want to reach for. So if we give that a shot, visit endpoint, and it still works. All right, two new helper functions in Laravel 9 that you should be aware of: str to operate on strings in a more object-oriented or even a pure way if you prefer, and then of course route that will return a redirect response to the given route name.
operate on strings in a more object-oriented or even a pure way if you prefer, and then of course route that will return a redirect response to the given route name.
