Mirpur University of Science and Technology: Deparment Software Engineering
Mirpur University of Science and Technology: Deparment Software Engineering
• The MVC
• Routing
• Views and Controllers
• Model Binding
• 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).
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’]);
• }
• 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”);
• URL::temporarySignedRoute(“invitation”,
now()->addHours(24),
[“invitation”=> 12345, “answer”=> “yes”]);
• The links can be protected using the “signed” middleware for blocking
unauthorized access.
• 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.
• 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”);
• Task::create(request()->only([‘title’, ‘description’]));
• One of the most common routing pattern is finding resource with given ID.
• Route::get(“conference/{id}”, function($id){
• $conference=Conference::findOrFail($id);