تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Why Use Frameworks0:50

the way. The simple reality is, when it comes to things like session handling, as we worked on here, building up a router, handling a response, creating a container, authentication, all of this stuff, validation, it goes on and on and on. The reality is, that work has been done. It's infrastructure work that, these days, none of us needs to be responsible for, unless we just happen to have that personality type where we have to control everything. Otherwise, trust me, there are better, more tested, more flexible versions of every single component we built here. So our little validator here, now, of course, this is mostly a proof of concept, but in reality, there are dedicated third-party validators that are highly tested, and can offer, or can work with any possible calculation you can think of. The same is true for building up a super-flexible router, or even these middleware that we created.

and can offer, or can work with any possible calculation you can think of. The same is true for building up a super-flexible router, or even these middleware that we created. Again, very, very simple implementation here, but there are, once again, third-party resources that take it to the next level. So yeah, everything we did in this series was to teach you the fundamentals, the basics of what these things are. Now, even if you don't use your own makeshift middleware, you understand the basic concept, this idea that it's almost like layers of an onion that can handle or respond to an incoming request, or an outgoing response. The same is true for session handling, and why would we reach for that, and when is it useful, and what's an example of using it in the real world? Okay, so let's do this. I'm going to switch over to the terminal, and like I said, I am going to remove with recursive force that demo project. Of course,

Choosing Laravel2:27

of using it in the real world? Okay, so let's do this. I'm going to switch over to the terminal, and like I said, I am going to remove with rm -rf that demo project. Of course, it's on GitHub, if you still want to refer to it. Okay, so now we're going to switch over to a dedicated framework. Of course, in the Laravel world, the two big shots in terms of frameworks are Laravel and Symfony. I recommend have a look at both. Peruse the documentation, figure out which one sort of fits your head and your way of understanding things a little better, and then go with that. Of course, down the line, even a few years from now, if you want to toy around with the other, that works as well. Okay, but of course, this is laracast.com. I have personally decided on Laravel, and that's what I would recommend to you as well. In fact, as it turns out, a lot of the decisions we made when we built our little micro framework were made with the understanding

Installing Laravel Project3:13

on Laravel, and that's what I would recommend to you as well. In fact, as it turns out, a lot of the decisions we made when we built our little micro framework were made with the understanding that if you graduate to Laravel, it will all instantly look at least 80% familiar. Okay, so with that in mind, why don't we install a fresh copy of Laravel, and then we'll take a look at the underlying directory structure. And I think you will be pleasantly surprised by how immediately familiar some of these terms and some of these directories are to you. All right, let's get going. I will go to get started. All right, why don't we skip down to your first Laravel project, and let's see. You should ensure that your local machine has php and Composer installed. Success, we've already done that. If you're developing on the Mac, they can be installed within minutes using a tool called Laravel Herd. Herd is a first-party or maybe second-party tool that,

we've already done that. If you're developing on the Mac, they can be installed within minutes using a tool called Laravel Herd. Herd is a first-party or maybe second-party tool that, well, does what it says on the tin. It creates a local environment for the Mac. It works great. It's what I use. I would highly recommend it. Otherwise, on Windows, if you need a refresher or you'd like to review some different installation options, yeah, just take a look here. Getting started on Windows, getting started on Linux, you'll learn about a tool called Docker, as well as a Laravel wrapper around Docker called SAIL. But yeah, otherwise, if you already have the basics installed on your local machine, you should have everything you need to go. Okay, let's switch back, and now we're going to create a new project. All right, so we'll do this directly through Composer. All right, I will paste that in, and yeah, this is going to create

Serving with Artisan4:43

Okay, let's switch back, and now we're going to create a new project. All right, so we'll do this directly through Composer. All right, I will paste that in, and yeah, this is going to create a new Laravel project, and why don't we call the folder Demo? All right, it's installing Laravel 10.2 at the time of this recording, and that'll take just a minute to pull in all of the dependencies. So now, if I switch over to our Demo project and I list all of the files, yeah, we're all set to go here. So take a look at this. Laravel ships with a command line utility called Artisan, and we can run it by using php and then the name of the tool, Artisan. And yeah, here you can find commands for all of the various operations and actions that you can run. And trust me, as I look at this, even to me, it's a little bit overwhelming, but like everything, 20% of these commands will be the ones that you run every day, probably even less, maybe 10%. But the others are there when

even to me, it's a little bit overwhelming, but like everything, 20% of these commands will be the ones that you run every day, probably even less, maybe 10%. But the others are there when and if you need them. Okay, so if I scroll to the very top, you will find one command called serve. This will serve the application on the development or the php development environment. So let's run that now, php artisan serve, and sure enough, we have a local server running here. All right, so now you can copy that or on my machine, I can command click that link. All right, and there we go. Congratulations, you now have your first Laravel install. All right, let's open this in our editor and take a look around. Okay, so I have opened up that demo folder that we just installed from the command line. And yeah, granted, there's a lot of files here, a lot of directories, it even goes further. And once again, it can immediately be a little bit overwhelming,

Routes, Views, Blade6:18

installed from the command line. And yeah, granted, there's a lot of files here, a lot of directories, it even goes further. And once again, it can immediately be a little bit overwhelming, but fight that. Trust me, all of these configuration files, you will reach for them only when you need to. And often when you are editing them, you are following instructions that tell you to edit them. So don't worry about this too much. However, what I do want to point your attention to is this, we have one directory called routes. All right, let's open that up. And I see web.php routes here. Yeah, we get this, we understand what's going on here, we built something very similar in our own project. And we can parse what's going on here. Well, we have a route, and we're going to listen for a GET request to this URI, the homepage. And then in response to that, it looks like we are returning a view called welcome.

Well, we have a route, and we're going to listen for a GET request to this URI, the homepage. And then in response to that, it looks like we are returning a view called welcome. Okay, where is this welcome view called? Well, at the time of this recording, Laravel stores views within the resources directory. So resources/views. Now, if you're watching this in the future, in 2024, 2025, it's possible that views might be a top level directory. Every once in a while, they make slight directory changes. But nonetheless, you'll figure it out. Alright, so let's open up views. And sure enough, I see our view. Again, think about how incredibly similar this workflow is to what we did throughout the series. So here's our view. And let's just do this, let's delete everything and replace it with the obligatory Hello, world. Alright, I'm going to go back to my browser, I will give it a refresh. And there we go. Everything's working. Okay, what about passing

and replace it with the obligatory Hello, world. Alright, I'm going to go back to my browser, I will give it a refresh. And there we go. Everything's working. Okay, what about passing data to the view? Well, once again, let's go to our route. And we can do that as the second argument, we'll say greeting is this time, universe. And now I will have a variable called greeting available in the corresponding view. Now, I can do it like this. And if I come back and give it a refresh, but of course, whenever you're using a framework, there will be dedicated helpers and shorthand and things like that, that you can reach for. So in this case, notice that the file name is welcome.blade.php. Now in our project, we called it welcome.view.php. But as it turns out with Laravel, Blade is a dedicated template engine that includes a few luxuries. And one of those is a shorthand for echoing out escaped variable values. So in this case,

turns out with Laravel, Blade is a dedicated template engine that includes a few luxuries. And one of those is a shorthand for echoing out escaped variable values. So in this case, I can use these two braces here. So now if I come back and give this a refresh, and yeah, it still works, of course. Okay, so in a general Laravel project, this is typically the approach you will take. Now, what about conditionals? Remember, in our series, we used this syntax. If some kind of value, and we even learned about a shorthand like this, well, if true, only on that condition do we display hello world. But of course, if it is a falsy value, then we should end up with a blank page. All right, well, once again, using Blade, there are shorthands, and we call these directives. It's basically just a little bit of sugar to make it easier to write, like this, @if. So provide your condition,

there are shorthands, and we call these directives. It's basically just a little bit of sugar to make it easier to write, like this, @if. So provide your condition, and then we can say @endif. Yeah, notice it's just a little bit easier to write. And as it turns out behind the scenes, Laravel is still going to compile this down to a raw vanilla PHP if statement. But yeah, this just makes it a little more, I don't know, easier on the eyes when you're working on it. Refresh, we see it. If we change it to false, of course, we return to a blank page. Okay, so now we've learned if nothing else, I can install Laravel. I know where I can create a route. I know how to respond to a route. So for example, if you want to create another one, let's listen for about. And that can return a view. It can load a controller. It can even return an array. It can return JSON, foobar. This is

if you want to create another one, let's listen for about. And that can return a view. It can load a controller. It can even return an array. It can return JSON, foobar. This is especially useful if you're building APIs. It's a little bit higher level than where we currently are. But nonetheless, just know that these things are options, as you see here. And here's the raw data that was returned. Otherwise, you can return simple strings, as you see there, or you can create a new view. So let's return a view called about. And then I know that by default, it's going to be located in the views directory. Okay, but what if we want to put it in a subdirectory like pages/about? Then you could do something like this, pages/, or you can even use dot notation, which corresponds to a /. Some people like that because they think it's, again, a little easier to write. All right, so create our

or you can even use dot notation, which corresponds to a slash. Some people like that because they think it's, again, a little easier to write. All right, so create our directory, our directory called pages, and then another one called about.blade.php. Now, in this case, I'm not even using master files or layout files. But when you stick with this, you will learn about all of the various things you can do with Blade, which are quite significant. We'll say about page. All right, I'm just giving you the 101 here. Come back, give it a refresh. And now we have a landing page for telling the User about ourselves. Very cool. Okay, so next, what else do we have here? We have the public directory. This is typically where you will store your images or your JavaScript files. And you'll remember, in our little framework, we also had an entry point that was responsible for sort of booting

This is typically where you will store your images or your JavaScript files. And you'll remember, in our little framework, we also had an entry point that was responsible for sort of booting up the framework. It pulled in Composer's autoloader. It handled routing the request. It caught any potential validation errors and things like that. And, of course, Laravel has one as well. Next, we have the app folder. And this is where you're going to spend most of your time. If you need to generate console commands, you can do it here. If you need to create dedicated controllers, you can do that here. So notice within our routes file, this is useful for very simple projects, typically. But I think you'll find for many projects, just having a simple callback function isn't enough. You need a little bit more structure. And that's where dedicated controllers come into play. So when you generate a controller, it will be stored here. And you

Creating a Controller12:37

callback function isn't enough. You need a little bit more structure. And that's where dedicated controllers come into play. So when you generate a controller, it will be stored here. And you will find that very easily you can migrate from a closure here or a callback to a controller. And why don't we do that now? Let's get rid of this to keep things simple. And why don't we create a WelcomeController? All right. We can do this from the command line. php artisan. And these will all be listed under the make namespace. So anything make is generating a file. You can generate a controller, an event, a factory, all of these various terms that Laravel offers. A mailable, a notification, a model. In our case, I want to make a controller. So let's do that now. php artisan make:controller HomeController. And why don't we call it HomeController? All right. So notice it goes directly within that controllers directory. And here's what I'm going to do. I'm going to take

php artisan make:controller. And why don't we call it HomeController? All right. So notice it goes directly within that controllers directory. And here's what I'm going to do. I'm going to take this right here, copy it, and move it into our new HomeController. So let's create a method here. And we will call this invoke. This is a common magic term for what we call single action controllers. So as it turns out, a controller can respond to multiple routes, or it can handle just a single action. I think you'll find in your projects, you probably will use both. But in this case, if you're only doing a single action, like we are in this case, we can call the method invoke, which is a magic PHP method name. All right. So now, though, if I switch back to our web.php file, we need to update this. So let's change it to say when you visit the home page, I want to load the HomeController. So we provide a path to the class, as you see there. Now, if I come back and

we need to update this. So let's change it to say when you visit the home page, I want to load the HomeController. So we provide a path to the class, as you see there. Now, if I come back and we return to the home page and give it a refresh, we get a blank screen. Let's go to our view. Oh, yeah. Of course, I was testing out Blade directives there. All right. Come back, refresh. And yeah, this time, we get the typical hello universe. Okay. But now we are using a dedicated controller to respond to the incoming request, which is really cool. Okay. So I'm going to stop right there. Because of course, we have a dedicated series at Laravel called Laravel from Scratch. And a quick little tip, you can visit laravelfromscratch.com to instantly access the latest version of that series. And yeah, it's going to start from the very beginning and tell you everything you need to know. It's a little bit long, but for good reason. It'll take you from

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟