codovel

We no longer support this blog. Please visit this site instead. https://laravelprojectlab.com

Saturday, June 18, 2016

How to use route, controller, model and view in Laravel

In this tutorial, I am going to show you how to create and use route, controller, model and view. I show you the basic only because it can make you easy to understand and remember. If you want to know more detail on how to use it, please go to https://laravel.com

Suppose that the project folder is C:\wamp\www\test_laravel
  • View
To create view, you have to create it in this folder C:\wamp\www\test_laravel\resources\views. And its extension is .blade.php (Ex: viewname.blade.php).
  • Model
In order to create model, you have to create it in this folder C:\wamp\www\test_laravel\app. And its extension is .php (Ex: Student.php) 
    • Model name should start with big letter and singular noun. Ex: Student.php, Customer.php, ProductCategory.php (2 words)
    • For its content, you have to create a class, and the class name should be the same to the filename. and it have to extends from Model.
 
    • The screenshot above mean that model Customer represents customers table in the database. The table name should be lowercase and plural noun. Ex: customers, users, product_categories (2 words)
  • Controller
In order to create controller, you have to create it in this folder C:\wamp\www\test_laravel\app\Http\Controllers. And its extension is .php
    • Controller name have to be in this format CustomerController (name of controller start with big letter + Controller)
    • For its content, you have to create a class, and the class name should be the same to the filename. and it have to extends from Controller
    • And inside class, we have to create methods and those method name have to start with get or post. Ex: getIndex(), getDelete(), postCreateCustomer() (2 words). Note: get is used for get request. And post is used to post request.
 
  • Route
 To use route in Laravel, you no need to create the file. Just go to edit the file in here C:\wamp\www\test_laravel\app\Http\routes.php

Route is front door to allow access to the system. So you cannot access view and controller without route.

This is the format of route.

Route::get('URI','Action')                  // Action can be function or controller method 
Route::controller('URI','ControllerName')

Ex: 
// route to view
Route::get('/',function(){
     return view('view1');
});

// route to controller action
Route::get('customer_list','CustomerController/getIndex');

// route to controller
Route::controller('customer','CustomerController');   // by default, it will run getIndex() method

How to run it? Go to browser and type
http://localhost/test_laravel         // it will point to route 1
http://localhost/test_laravel/customer_list              // it will point to route 2
http://localhost/test_laravel/customer                  // it will point to route 3. And method getIndex() will execute

1 comment:

Micheal Alexander said...

I got here much interesting stuff. The post is great! Thanks for sharing it! Angularjs Tutorials Online