Saturday, June 3, 2017

Laravel From Scratch 3

.

.

BASIC ROUTING & CONTROLLERS


1) INSERT ROUTE EXAMPLE

edit file: lsapp/routes/web.php

file content:
<?php
Route::get('/', function () {
    return view('welcome');
});
Route::get('/hello',function(){
return '<h1>Hello World</h1>';
});



2) INSERT PAGE EXAMPLE

edit file: lsapp/resources/views/pages/about.blade.php

file content:
About

edit file: lsapp/routes/web.php

file content:
<?php
Route::get('/', function () {
    return view('welcome');
});
/*
Route::get('/hello',function(){
return '<h1>Hello World</h1>';
});
*/
Route::get('/about',function(){
return view('pages.about');
});


3) INSERT URL PARAM EXAMPLE

edit file: lsapp/routes/web.php

file content:
<?php
Route::get('/', function () {
    return view('welcome');
});
/*
Route::get('/hello',function(){
return '<h1>Hello World</h1>';
});
Route::get('/about',function(){
return view('pages.about');
});
*/
Route::get('/users/{id}',function($id){
return 'This is user ' .$id;
});


4) USING ARTISAN

cmd: php artisan make:controller PagesController

edit file: lsapp/app/Http/Controllers/PagesController.php

file content:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function index(){
return 'INDEX';
}
}


edit file: lsapp/routes/web.php

file content:
<?php
/*
Route::get('/', function () {
    return view('welcome');
});
Route::get('/hello',function(){
return '<h1>Hello World</h1>';
});
Route::get('/about',function(){
return view('pages.about');
});
Route::get('/users/{id}',function($id){
return 'This is user ' .$id;
});
*/
Route::get('/', 'PagesController@index');



5) CONTROLLER RETURNING VIEW

edit file: lsapp/resources/views/pages/index.php

file content:
<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>{{config('app.name','LSAPP')}}</title>
    </head>
    <body>
            <h1>Welcome To Laravel</h1>
            <p>This is the laravel application
            from the "Laravel From Scratch" Youtube series</p>
    </body>
</html>


edit file: lsapp/resources/views/pages/about.php

file content:
<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>{{config('app.name','LSAPP')}}</title>
    </head>
    <body>
            <h1>About</h1>
            <p>This is the laravel application
            from the "Laravel From Scratch" Youtube series</p>
    </body>
</html>



edit file: lsapp/resources/views/pages/services.php

file content:
<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>{{config('app.name','LSAPP')}}</title>
    </head>
    <body>
            <h1>Services</h1>
            <p>This is the laravel application
            from the "Laravel From Scratch" Youtube series</p>
    </body>
</html>



edit file: lsapp/app/Http/Controllers/PagesController.php

file content:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function index(){
return view('pages.index');
}
public function about(){
return view('pages.about');
}
public function services(){
return view('pages.services');
}
}



edit file: lsapp/routes/web.php

file content:
<?php
/*
Route::get('/', function () {
    return view('welcome');
});
Route::get('/hello',function(){
return '<h1>Hello World</h1>';
});
Route::get('/about',function(){
return view('pages.about');
});
Route::get('/users/{id}/{name}',function($id,$name){
return 'This is user '.$name.' with and id of '.$id;
});
*/
Route::get('/', 'PagesController@index');
Route::get('/about', 'PagesController@about');
Route::get('/services', 'PagesController@services');
.

NEXT: Laravel From Scratch 4

No comments:

Post a Comment

Labels