0% found this document useful (0 votes)
23 views

Mirpur University of Science and Technology: Deparment Software Engineering

Uploaded by

ahsan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Mirpur University of Science and Technology: Deparment Software Engineering

Uploaded by

ahsan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MIRPUR UNIVERSITY OF SCIENCE AND TECHNOLOGY

DEPARMENT OF SOFTWARE ENGINEERING


Model View Controller – MVC
(lecture # 15)

Engr. Muhammad Raees


(Lecturer)

Date: April 23, 2020


LECTURE CONTENTS

• The MVC
• Routing
• Views and Controllers
• Model Binding

Web Design & Development 3


MVC – Revisited

• Model
• Represents an individual database table (or a record from the database table)
– such as “Company”, “Dog”
• View
• Represents the template that output your data to the end user – such as a login
page, with given set of HTML, CSS and JavaScript
• Controller
• Like a traffic cop, takes the HTTP requests from the browsers, gets the right
data out of the database (model), validates the user input, and eventually
sends backs a response to a user (inside a view).

Web Design & Development 4


HTTP Methods

5
Web Design & Development
Routing
• MVC applications use the routing system for URLs map to controllers and actions.
• A route is a rule that is used to decide how a request is handled.
• You can request any of the following URLs, and they will be directed to the Index action
on the HomeController:
• /
• /Home
• /Home/Index
• Routing (like real routes) is method to specify the Paths/URLs/Resources of any web
application.
• For PHP API: routes/api.php
• For PHP WebApp: routes/web.php
• Route::get (‘/’, function(){ //root route
• return “Hello World”;
• }
Web Design & Development 6
Some Routing Examples
• Route::get('/', 'HomeController@index');
• Route::get('Home', 'HomeController@index’);
• Route::get('Services/{id?}', 'HomeController@Services')->middleware('verified’);
• Route::get('contactus', function(){
• return view('contactus');
• });
• Route::post('/Blog/posts/{post}/comment', 'HomeController@MakeComment')-
>middleware('auth’);
• Route::resource('EditProfile', 'UserController')->middleware('auth’);
• Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'],
function() {
• Route::resource('/posts', 'PostController', ['middleware' => 'admin’]);
• }

Web Design & Development 7


Route names

• The simplest way to refer to the routes anywhere in web application is by using
them as relative or absolute URL paths. However, routes can also be used by their
names.
• Route::get(“Home/New”, “HomeController@New”)->name(“home.new”);

• This is how the above route can be accessed.

• <a href="{{ route(‘home.new') }}“ />

• {{ }} sign is equivalent to <?php … ?> for php code in Laravel.

Web Design & Development 8


Signed Routes
• Signed routes are used for sending notifications, such as password reset, accepting
invitation, verifying account etc.

• URL::route(“invitation”, [“invitation”=> 12345, “answer”=> “yes”]);

• URL::signedRoute(“invitation”, [“invitation”=> 12345, “answer”=> “yes”]);

• URL::temporarySignedRoute(“invitation”,
now()->addHours(24),
[“invitation”=> 12345, “answer”=> “yes”]);
• The links can be protected using the “signed” middleware for blocking
unauthorized access.

Web Design & Development 9


Views
• Views are returned by routes directly (as seen above) or the controllers return the
views by processing the requests.
• Views are the files describing how the output will look like.
• There are two types of views in Laravel.
• Plain php views
• Blade template views
• Views can be simple or embedded with arguments.
• Route::get('offers', function(){ return view('frontend.offers');
• });
• $posts = Post::with(['user', 'category', 'tags', 'comments'])->published()-
>paginate(10);
• return view('blog', compact('posts'));

Web Design & Development 10


Controllers

• All the routes map to actions defined in controllers. In the MVC pattern, incoming
requests are handled by controllers.
• Controllers are PHP files (usually inheriting from the App/Http/Controller, which
is the built-in MVC base).
• Each public method in a controller is known as an action method, meaning you
can invoke it from the Web via some URL to perform an action.
• Open the existing controller to view the methods.

• php artisan make:controller ControllerName


• Command to create new controllers inside a Laravel project.

Web Design & Development 11


Getting Input from User

• The second most common task of controllers is to get input from the users, which
is provided through an html form. The input is collected and stored/used through
“post” action of webpage and route.

• Route::post(“tasks”, “TaskController@store”);

• The input is collected through the request created by the browser.

• Task::create(request()->only([‘title’, ‘description’]));

Web Design & Development 12


Resource Controller

• Sometimes naming controller actions can be the hardest thing.


• Laravel provides predefined names for controller methods being used for CRUD
applications, called resource controllers.

• php artisan make:controller ControllerName --resource

• This can be viewed in routes defined above.

Web Design & Development 13


Route – Model Binding

• One of the most common routing pattern is finding resource with given ID.

• Route::get(“conference/{id}”, function($id){
• $conference=Conference::findOrFail($id);

• The above method is called implicit route binding.


• Otherway, a custom response can also be sent using the response methods
provided by the Laravel.

Web Design & Development 14


References

• Chapter 3, Laravel_ Up & Running. A Framework for Building Modern PHP


Apps by Matt Stauffer – (2019, O’Reilly Media).

Web Design & Development 15


THANKS

You might also like