Laravel Basics
Steps to be Done
- Artisan command line tool
- Artisan command to generate boiler plate code for a controller
- Basic routing
- Call a controller method from a route
- Passing variables from controllers to views
Artisan command line tool
Artisan is a command line tool that automates common tasks in Laravel. The tool is run from the command line. The artisan command line tool can be used to perform the following tasks and many more- Generate boilerplate code easily create controllers, models etc.
- Database migrations migrations are used to manipulate database objects. Migrations can be used to create and drop tables etc.
- Database seeding seeding is a term used to add dummy records to the database.
- Routing
- Application configuration
- Run unit tests
How to use the artisan command line tool
- Open the command prompt.
- Run the following command
in cmdcd C:/xampp/htdocs/laravel
php artisan list
You will get a list of all available commands.Artisan command to generate code for a controller
php artisan make:controller Hello
HERE,php artisanis used to run the artisan command line tool- make:controller Hello specifies the command that the tool should run. This command will create boiler plate code for a controller Hello in /app/Http/Controllers/Hello.php
Open
/app/Http/Controllers/Hello.phpYou will see the following code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class Hello extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
* @return Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
HERE,
namespace App\Http\Controllers;defines the namespace for our controlleruse Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller;imports namespaces with the required classes to run the controllerclass Hello extends Controllerdefines the class for the controller. The controller extends the base controllerpublic function index(){}defines the default function for the controllerpublic function create(){}defines the function that can be used to create new resources such as new record in a database tablepublic function store(Request $request)defines the function used to store a newly created resource in storagepublic function show($id)defines the function that retrieves a single resource based on the supplied id.public function edit($id)defines the function used to retrieved a record based on the supplied id for editingpublic function update(Request $request, $id)defines a function that is used to update a resource in storagepublic function destroy($id)defines the function used to remove a resource from storage
Basic routing
In this section, we will create a new route that will display hello world in the web browser- Open web.php file. The file is located in
/app/routes/web.php - Add the following code to routes.php
Route::get('/hello',function(){
return 'Hello World!';
});
HERE,Route::get('/hello',function});responds to the GET method of HTTP for the URI hello.function()defines an anonymous function that does the actual work for the requested URI.-
return 'Hello World!';returns Hello World! to the requesting browser.
-
Go to the web browser
- Open the following URL
http://localhost/larashop/public/hello
You will get the following outputHello World!
Route to controller
For the sake of reference, we will comment the above code as follows/*Route::get('/hello',function(){
return 'Hello World!';
});*/
Add the following code to /app/Http/routes.phpRoute::get('hello', 'Hello@index');
Open /app/Http/Controllers/Hello.phpAdd the following code to the index function
public function index()
{
return 'hello world from controller : )';
}
Save the changesOpen the following URL in your browser.
http://localhost/larashop/public/hello
You will get the following messagehello world from controller : )
Loading a view from a controller
In a typical application, a controller will have to load a view. In this tutorial, we will- Create a view that will be loaded from the controller
- Pass a variable to the view and modify
-
Add a new route to
/app/Http/routes.phpthat will pass in the Hello world view
-
create a new file hello.blade.php in
/resources/views/
- Add the following code to
/resources/views/hello.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel</title>
<link href="//fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 96px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Hello {{$name}}, welcome to Laraland! : )</div>
</div>
</div>
</body>
</html>
HERE,Hello {{$name}}, welcome to Laraland! : )is the text that is displayed in the browser.{{$name}}prints the value of the name variable. Open/app/Http/routes.php
Route::get('/hello/{name}', 'Hello@show');
HERE,Route::get('/hello/{name}', 'Hello@show');directs the URI GET request for/hello/valuetoshow()function in Hello controller.
app/Http/Controllers/Hello.phpModify
show($id){...} to the followingpublic function show($name)
{
return view('hello',array('name' => $name));
}
HERE,public function show($name)defines a show function that accepts a parameter $namereturn view('hello',array('name' => $name));loads a view namedhello.blade.phpand passes a variablenameto the view. Note: we did not specific the extensions .blade.php. Laravel takes care of that for us.
http://localhost/larashop/public/hello/rad
Comments
Post a Comment