Thread Contributor: Baredee[TUT] 3 - Artisan / Routes / Blade - Laravel Tutorial Series
#1
If you didn't catch the last tutorial, you can view it here

~~~~ Hey, everyone! ~~~~


Welcome to part 3 of the Laravel TuT series - we're going to begin with Endpoints





Before that...

Just make sure you're running WAMP apache and mysql.

Also let's boot up our actual server

How?

1) Open up a CMDL/Terminal and CD to your project's directory...
2) Make sure you're in the ROOT of your project directory (gets me every time)
3) run "php artisan serve"
4) observe



[Image: 6e9155db685a4da3a312375a6c35a108.png]


It's not lying... visit http://127.0.0.1:8000 or http://localhost:8000 and look...

A fresh install of laravel.





Endpoints and Wildcards

Open vtech/routes/web.php and you should see the following content:


[Image: 2f81777a076540d68a416f7936e48515.png]


that's an endpoint... it's something that handles a visit to a specific location in your website.
we may also refer to it as a 'route'.


Think of an endpoint like a destination in your website that a user can potentially visit.

For example... if we were a running a basic PHP website, and a visitor visits it -> http://website.com/index.php
they would have visited the index endpoint.

Similarly, if a user visits http://website.com/members ... they've now hit the /members endpoint, and so on.

You may have seen a URL like the following in some MVC or MVC-like websites: http://mvc-site.com/members/1

Assuming you're a decently technical person, you'll know that the website could be displaying a specific member who
happens to have an ID of 1.

But unlike the index and /members endpoints - the ID of a member could change depending on the member that's
being viewed at the current time. For that reason, we can't say that the endpoint is /members/1 or /members/2 ...

Rather, we will create a placeholder, an X-value, an anonymous field, call it whatever you may. But know that it is referred to as a 'wildcard'.

The layout of the endpoint would therefore be /members/{id} - with {id} being replaced by whatever User-ID is passed in through.


...a breather...


Now, what we've just covered could be easy MVC logic for some, and it could be rocket science for others. Take your time, digest the information,
and if you think you may have misunderstood something, feel free to ask me and I will clarify, as I'm not the best at explaining things :)





How it works - the process


So - we go to our website...

1) http://localhost:8000/

2) The following endpoint captures requests made to "/" (i.e. root), which is what the URL is hitting, the root of the public interface, right?


[Image: 2f81777a076540d68a416f7936e48515.png]


3) Our route/endpoint basically is saying: when you receive a request on the root of the website ("/"), return the 'view' named 'welcome'.

4) Laravel goes to the location for views and looks for 'welcome' (/resources/views/welcome.blade.php) (don't worry, we'll talk about `blade`)

5) If we open that file, this is what we see:

[Image: c52cdabd0ec94d5eac2fff2f1b7207f8.png]

See how it works?


If we change our endpoint to this:

[Image: 8905fbe44eac4289bbe944a3fa2b3258.png]


Guess what happens when we visit the website...


[Image: df1a939614d04862a123e4f725d9fcb3.png]


Exactly.

I hope that gives you some understanding of how routes are essentially the handlers of requests, once an endpoint has been hit (visited)





What's 'blade'?

Blade is Laravel's template language.

With it, you can extend templates (create one master template, and branch out into other pages)


[Image: cedadd39c80e41a4aada1260e6332c2b.png]


You can dynamically put in information in HTML, like this:


[Image: fb8d52d4d9ef487b8d7067a4491ab9f4.png]


Youuu cannnnnnnnn ... uhh .. you can display various components based on conditions being met, so for [email protected]


[Image: cdfcd0ff2ff64ce0a5523c823d813a85.png]



You can loop through objects.



[Image: c784a6cdb7384f8da465e8d69ffffbf1.png]



... and much much more.

Blade is really powerful.

Use blade.





Let's make our own route


I'm going to add my own route to the web.php file:


[Image: 5224d6b495d34b86939a6741f82faaba.png]


Now if I visit http://localhost:8000/people ...


[Image: d91b95de8ace44be85a9575bd042d7cf.png]



Let's dedicate a few minutes to imagening a scenario, and this will save us a lot of time.

Imagine, that after I visit the /people endpoint, I want to fetch people from the database.

and let's say people consist of these fields -> "id", "name", "age", ok?

Imagine having to retrieve that data from the database, modifying whatever needs to be modified,
and returning it back to the view, every time. Ok? We got that?

Now imagening implementing a CRUD functionality:

Creating people records,
Reading/Retrieving people,
Updating/Modifying people,
Deleting/Removing people

ALL within the endpoint that we've created... that little space where we currently have 'die("show me people! :D");'

Yeah?

Now imagine we had endpoints for not only people, but products, categories, accounts, pages, and what not.

Now very nice.

For that reason, we use Controllers to handle that.

For each respective collection or data (i.e. users, products, blog posts) we'll create a Controller.

The controller will be responsible for managing the CRUD functionalities of data pertaining to whatever it represents.

We will also create a Model for each of those collections.

The model will simply represent the record in the database, meaning it will be like an object consisting of all the fields
that the record has in the database.

So basically,


Database:

Table name: people

id | name | age
1    John   34
2    Alex   59
3    Sam    18
4    Ahmed  24

Because of Eloquent, we're actually able to do the following

$person->id,
$person->name,
$person->age ... you see?

Nowhere do we create those properties, nor the class "Person" ...but we DO create a model

Behind the scene, Laravel converts database fields into properties for our model, which then allows us to access them as I've shown above.

Meanwhile the controller will take care of functions for actually interacting with the database, returning information, redirecting, and so on.






Final thing

Before we conclude I would like to do one thing.

Let's associate our newly-created endpoint with a Controller that we will create in the next tutorial.



[Image: 54ad9fffe1d74a4d8a69104c285497c6.png]

^ the "/" before "people" is optional btw.

Also, that is how we register a specific endpoint for a controller.

Know that a controller needs to be named in this fashion -> NameController, with "Controller" as a suffix.

The "@index" part tells the route to basically refer to the controller named "People", and in it, assumes there's a function
named "index" - and Laravel will proceed to run that function.

Of course if we visit our endpoint now, we see this:


[Image: 9dea61c0937f438daad5f5370ad790f2.png]


Yep.

We'll create that controller in the next tutorial.





Conclusion

I hope you enjoyed this part of the tutorial. In the next one, we'll create a controller and a model for Person/People - and we'll hopefully
gain a deeper understanding on how Laravel works.

If you have any questions, feel free to post them below and I'll try to get to them.

Peace.




[Image: a701e0ca783f4720a2616a0d57f28e3b.png]



Possibly Related Threads…
Thread Author Replies Views Last Post
[TUT] 7 - Updating & Deleting / Wrapping up - Laravel Tutorial Series Baredee 6 5,206 05-12-2017, 10:59 AM
Last Post: Baredee
[TUT] 6 - Form->Database - Laravel Tutorial Series Baredee 6 4,727 03-30-2017, 12:00 AM
Last Post: Baredee
[TUT] 5 - Migrations & Schemas - Laravel Tutorial Series Baredee 0 1,304 03-01-2017, 06:46 PM
Last Post: Baredee
[TUT] 4 - Models & Controllers - Laravel Tutorial Series Baredee 0 1,361 02-28-2017, 06:28 PM
Last Post: Baredee
[TUT] 2 - Installation - Laravel Tutorial Series Baredee 0 1,459 02-27-2017, 03:28 PM
Last Post: Baredee

Forum Jump:


Users browsing this thread: 1 Guest(s)