Need Help Understanding MVC with Laravel for Web Dev Project.

XenitXTD

Well-Known Member
Joined
Sep 26, 2014
Messages
104
Hi Guys,

I am Pretty New To Developing in PHP + Laravel, I was recently learning to Code Raw PHP when I decided to Invest in Laravel after reading up on it...
I have made some head way in understanding the Routes and Blades etc... and so forth... However While I somewhat understand MVC I am not 100% clear on it...

From what I have read this is how I interpret it and ask if you would take the time to correct me where I stray of the MVC Path...

First in Understanding the Model, I understand that one creates a Model per Table in the Database,
Inside the Model one will define the Functions that Retrieve the Data from the model in Defined methods

For Example:

we have a table (Users)

so we will create several methods which can be called to get data from the table in controlled ways
- function All_Users ();
- This will return all users from the table
- function get_Users ($id);
- This will return users based on the parameter defined $id


We can Create several functions which will be called from the controller to get the data in several different ways. Including Removing Information etc.
- Like top_ten(), last_entry() etc as required by website... and so on etc.


Second is the Controller, The Controller which is called from the view will then have its CRUD operations or Several Variations of specific CRUD operations that
will call on the methods defined in the model as follows

For Example:
Using the Model Above we will call the model then its method All_Users () as follows
$Users->All_Users();

This will simply Get the data from the model as predefined in the model

As I understand it the Controller is there to communicate between a view and 1 or more models...
Meaning in the case that I had a dashboard and wanted to see the users with the roles and the Permissions I would have a dashboard controller that
makes calls to the models
* Users
* Roles
* Permissions

Then it will pass the information back to the Dashboard view and the view will manipulate the Data and present it in the desired format and design.

The Controller should be as light as possible so it should not manipulate the data which should be the task of the model and should just be passing
requests from the view to the model and the data back to the view from the model

The Controller will have CRUD Methods which will simply define what Models and Methods of the Models should be executed to get the results it needs
and then the results are passed back to the view to be displayed.

(From My Novice Point of View I would create a Controller Per View in the Case where the particular View needs to communicate with the database)

Third is the View, The View is what the user sees, It makes any requests from user input and processes it by making the relevant calls to the Controller and
then or any request without user input manipulates it to present the results to the user to see...

The View never connects directly to the data or the model and always goes via the Controller...



Please provide me with any clarity if my understanding it incorrect... As I am new to MVC, I have been reading about it for some time but this is the first time I am using it. I have read a few books as well but most people who write about MVC or any other assume that anyone who is programming has used it in some form or another before... And this is not the case...
I can easily learn it as i go along but if it don't understand it correctly I might become accustomed to using it the wrong way and then it will be hard to change those ways later on.
 

_kabal_

Expert Member
Joined
Oct 24, 2005
Messages
3,986
will read this in full later, but please read PSR-1 and PSR-2 at http://www.php-fig.org/ regarding coding standards and styles, specifically around method and variable names.

in MVC, the model is not always a single object. the model is a layer. the model could consist of the domain object (User), the service (UserService/Manager), and the data access object(dao)/repository.
 

Compulutions

Compulutions representative
Company Rep
Joined
Mar 4, 2012
Messages
12
Hello

You have a good understanding of how things work and why but it is indeed slightly warped. You are correct making the choice that you did in moving from flat PHP to an interactive framework. It would be bad if you were to continue on with a mixed understanding of the entire process.

You should stop, take time to learn more and really get an understanding of things, then move on with any substantial project, which you're doing so good choice again.

You could start by reading about relationships in Laravel.

Kabal is right and I am sure that he will provide you with some great understanding but we can try to help a little too.

The model is indeed a layer.

Here are some things that I noticed:

I understand that one creates a Model per Table in the Database

Pretty much correct. For instance, you might have a User model that extends laravels data model for core functionality and implements certain interfaces for required / helpful functionality.

Inside the Model one will define the Functions that Retrieve the Data from the model in Defined methods

Not really correct, remember that Eloquent does almost everything for you and by creating the model you are making the data available. All we need to do is tie that data to something, so we create a method that does just that.

This is where relationships come into play. You could say that user has many... Posts (in a blog)? So you'd have a method inside of your User model that says exactly that.

You'd then have a posts table in your database with a key that links to the id value of your users table.

You'd then create another model to get the posts data.

This is the other side of the relationship. You would create a method that says that one post belongs to one user making the user data available to the Post model.

Personally, I use the model to store the form specific validation rules too, don't think it's 100% correct but its a good place to keep it thinking logically...

The Controller which is called from the view

The controller will contain the CRUD operations. It would get data from the model required by the view. it would then render the view which would have the model data available via the method we created in the Models.

The posts controller as mentioned would have crud functionality, so you could create methods in it to create, read, update and delete.

You might also have a method like getIndex to list all posts?

Inside of this method you would define a variable that uses the Post model method and then uses the Eloquent model method "all" to retrieve all of the posts available.

PHP:
$posts = Post::all();

Then all that is left to do in this method is render / make a view to list all posts.

the view will then have its CRUD operations

The view would then have available the variable data we make earlier and you could use it in blade like:

PHP:
@foreach($posts as $post)

If you setup the crud functionality and routes correctly that is all that is needed, when you visit the create view, you will have available the data that you have specified within the controller method that relates.

Just remember that within this, Laravel has pretty much everything covered already... There is a method for everything so you don't actually need to create code to do an update for instance... You would just use the update() method on the $data that you get by posting the form.

Hope that helps, good luck!
 

XenitXTD

Well-Known Member
Joined
Sep 26, 2014
Messages
104
Thanks so much for your time @Compulutions ... This helps me considerably in understanding MVC.

I will definitely Read a bit more in depth on eloquent and relationships in laravel... And then Try a few Basic Scenario's before I take on the Bigger task I have planned for myself...
 

XenitXTD

Well-Known Member
Joined
Sep 26, 2014
Messages
104
@_kabal_ I look forward to your answer as well as you mentioned

the model could consist of the domain object (User), the service (UserService/Manager), and the data access object(dao)/repository.

This has me a little lost but would be nice to know what is meant by this...

:p
 

Compulutions

Compulutions representative
Company Rep
Joined
Mar 4, 2012
Messages
12
@_kabal_ I look forward to your answer as well as you mentioned



This has me a little lost but would be nice to know what is meant by this...

:p

Looking forward to it too.

Very simply though, think about my example. In the Post model, you're simply creating a method to tie the data to.

Where is the data coming from, how is it able to be referenced?

Then take a step back... And think about the broader picture and you will see hey wait, there is a specific set of needs that have to be accounted for. These are being accounted for somewhere and I mentioned like with the model you would be extending the Eloquent class to provide you with core functionality and then implementing certain interfaces in order to account for any additional functionality that may be required.

In flat PHP, think of everything that you need to do in order to maintain a working authentication system? It'll need that functionality, etc, won't it?

How are these things getting from point A to point B, they need a method of transport, we use sessions and such right? Well think about everything that you need in order to be able to maintain this aspect too.

Interfaces!
 

zippy

Honorary Master
Joined
May 31, 2005
Messages
10,126
if you have access to iTunes the first video in the Stanford iOS course explains MVC very nicely.

It is from an objective c perspective, but I think its relevant to any language. One of the best explanations of MVC that I have seen.
 

XenitXTD

Well-Known Member
Joined
Sep 26, 2014
Messages
104
Very simply though, think about my example. In the Post model, you're simply creating a method to tie the data to.

Where is the data coming from, how is it able to be referenced?

Then take a step back... And think about the broader picture and you will see hey wait, there is a specific set of needs that have to be accounted for. These are being accounted for somewhere and I mentioned like with the model you would be extending the Eloquent class to provide you with core functionality and then implementing certain interfaces in order to account for any additional functionality that may be required.

In flat PHP, think of everything that you need to do in order to maintain a working authentication system? It'll need that functionality, etc, won't it?

How are these things getting from point A to point B, they need a method of transport, we use sessions and such right? Well think about everything that you need in order to be able to maintain this aspect too.

Interfaces!

@Compulutions I am ... I am just not a fast learner but once i have it, it sticks... i tend to get quite technical in needing to fully understand something before i can use it...

I want to create a Laravel MVC application with a secure login (Roles, permissions, module access etc) which will ultimately have a API for mobile as well... so I am reading up like crazy to get it all down... I do get impatient sometimes but i am fighting the urge to start before I am in full understanding of how things work...
 

Compulutions

Compulutions representative
Company Rep
Joined
Mar 4, 2012
Messages
12
fighting the urge to start before I am in full understanding of how things work

Don't fight it... You will regret it later on... It might take you an extra 3 months, or 6 months to learn everything you need to know but then, you are going to have a real product. It won't be something that is makeshift and it won't cause you all those extra hours and grief when you realize that there are 101 things to change to make it as it should be. Then you have a mess and you will have regrets and be like oh dammit i've lost so much time now... It's not nice :)
 

Ludjer

Well-Known Member
Joined
Jul 12, 2005
Messages
400
Personally i have a subscription to laracasts, costs 9$ a month but lots of very informative videos on how to do things right. Also they have nice full blown video tutorials to create full applications that take you through the whole development life-cycle using laravel. If you dont want to drop $9 then they do have a few free videos available.
 

XenitXTD

Well-Known Member
Joined
Sep 26, 2014
Messages
104
Thanks Guys for all the replies, As soon as i get a breather from work i am gonna dive right in
 

Ludjer

Well-Known Member
Joined
Jul 12, 2005
Messages
400
Rails is good but you need to know ruby to code it, also OP said he was doing raw PHP code which means he already has experience with PHP which means laravel is one of the best php frameworks out there to learn. And node.js is interesting but i believe it still has a bit of maturing to do. Also between ruby and node, i would say PHP has the most amount of tutorials and resources to get started with. Also its a nice and easy language to use.
 

semaphore

Honorary Master
Joined
Nov 13, 2007
Messages
14,000
Rails is good but you need to know ruby to code it, also OP said he was doing raw PHP code which means he already has experience with PHP which means laravel is one of the best php frameworks out there to learn. And node.js is interesting but i believe it still has a bit of maturing to do. Also between ruby and node, i would say PHP has the most amount of tutorials and resources to get started with. Also its a nice and easy language to use.

God forbid someone learn a new language. As for node it is production ready, perhaps glance at some of the big names using it.
 

nand

Senior Member
Joined
Nov 2, 2012
Messages
742
@OP, it seems you're looking for design pattern advice more than Laravel itself.

MVC isn't being implemented in a pure fashion anymore. Laravel has many more components thrown into the mix, but the fundemental is still important. I suggest reading gang of four, and martin fowler.

One last note, Laravel uses Eloquent, which implements the ActiveRecord pattern. I suggest reading up on it, since it's quite different from the data mapper patterns being assumed by others in the thread.

Rails is good but you need to know ruby to code it, also OP said he was doing raw PHP code which means he already has experience with PHP which means laravel is one of the best php frameworks out there to learn. And node.js is interesting but i believe it still has a bit of maturing to do. Also between ruby and node, i would say PHP has the most amount of tutorials and resources to get started with. Also its a nice and easy language to use.

You wouldn't believe me even if I told you what's being done written in Node.js.
From Wal-mart to BoA, and everywhere in between, it's being used to scale hilariously.
 

Ludjer

Well-Known Member
Joined
Jul 12, 2005
Messages
400
God forbid someone learn a new language. As for node it is production ready, perhaps glance at some of the big names using it.

if he wants to learn and understand MVC its best to do it in a language he knows then its less of a learning curve. Also node comes with allot of anti-patterns and bad coding concepts and unless you already understand how to code in other languages there is a very high chance you will bring these bad habits over from node. That's why i highly advise against node.js for any new coders.

@OP, it seems you're looking for design pattern advice more than Laravel itself.

MVC isn't being implemented in a pure fashion anymore. Laravel has many more components thrown into the mix, but the fundemental is still important. I suggest reading gang of four, and martin fowler.

One last note, Laravel uses Eloquent, which implements the ActiveRecord pattern. I suggest reading up on it, since it's quite different from the data mapper patterns being assumed by others in the thread.



You wouldn't believe me even if I told you what's being done written in Node.js.
From Wal-mart to BoA, and everywhere in between, it's being used to scale hilariously.

yes i never said it is not being used, just like all the new technologies the things you can do with them is insane. I have coded a few applications with node i know how powerful it is. But it is still very new just a couple years, also resources available is allot less then the other programming frameworks out there.

some facts about node js:
Current Version: v0.10.32 <- its not even v1 yet
Node.js does not teach good programming habits, there for its good to avoid if your new to coding.
The concurrency model is absolutely terrible.

the really good:
I like it for one thing, sockets, sending and receiving chat like communication to a web browser.
Delayed database updates.

go look up why there is so much hate for it. Its an amazingly fast framework for certain applications but it does not fit in everywhere. Here is a good article explaining why and how it should be used: http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js
 

semaphore

Honorary Master
Joined
Nov 13, 2007
Messages
14,000
if he wants to learn and understand MVC its best to do it in a language he knows then its less of a learning curve. Also node comes with allot of anti-patterns and bad coding concepts and unless you already understand how to code in other languages there is a very high chance you will bring these bad habits over from node. That's why i highly advise against node.js for any new coders.

He should learn the theory first and understand that, languages can be picked up fast. A good developer should have multiple toolsets to leverage on.

some facts about node js:
Current Version: v0.10.32 <- its not even v1 yet
Node.js does not teach good programming habits, there for its good to avoid if your new to coding.
The concurrency model is absolutely terrible.
Ahh the lovely mental block of the great lovely 1.0 version. We have software in our environment sitting on v0.5.1, you're telling me because we didnt version it 1.0 its not stable ? :erm:

the really good:
I like it for one thing, sockets, sending and receiving chat like communication to a web browser.
Delayed database updates.

So you like it only because it can make chat apps. lol. :erm:

go look up why there is so much hate for it. Its an amazingly fast framework for certain applications but it does not fit in everywhere. Here is a good article explaining why and how it should be used: http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js

Nobody said it fits for everything.

Call back hell can be reduced using fibers and futures.

Code:
var placement = (function(date) {
  var conn = db.connect().wait();
  conn.query("select blah from x < ?",  
             [date]).wait().forEach(function(x) {
    conn.execute("insert into moo", 
                 [x.id, ...]).wait();
    conn.execute("delete from x where b=?", 
                 [x.id]).wait();
  });
  console.log("blah done");
}).future();
 
Top