Laravel 5 Introduction to the Framework (iii), Introduction to the Laravel framework
In this tutorial, we'll use the Laravel 5 's out-of-the-box Auth system to authenticate our backend, and build a front page to showcase pages.
1. Permission Validation
The background address is http://localhost:88/admin, and all of our background operations will be performed under this page or its sub-pages. With the Auth provided by Laravel 5, we only need to change a very small portion of the routing code to implement the permission validation function.
First, change the routing group's Code to:
Copy the Code code as follows:
Route::group ([' prefix ' = ' admin ', ' namespace ' = ' admin ', ' middleware ' = ' auth '], function ()
{
Route::get ('/', ' adminhomecomtroller@index ');
Route::resource (' pages ', ' Pagescontroller ');
});
There is only one change in the above code: the first argument to ' Route::group () ' (an array) adds an ' middleware ' = ' auth '. Now visit http://localhost:88/admin, you should jump to the landing page. If there is no jump, do not panic, from the top right corner exit, re-enter can be.
Our personal blog system does not want people to register casually, below we will change some of the routing code, only the basic login, logout function.
Delete:
Copy the Code code as follows:
Route::controllers ([
' Auth ' = ' auth\authcontroller ',
' Password ' = ' Auth\passwordcontroller ',
]);
Increase:
Copy the Code code as follows:
Route::get (' Auth/login ', ' auth\authcontroller@getlogin ');
Route::p ost (' Auth/login ', ' auth\authcontroller@postlogin ');
Route::get (' auth/logout ', ' auth\authcontroller@getlogout ');
The background of the minimized feature with permission validation has been completed, and this background currently only manages page (page) resources. Next we'll build the front page to show pages.
2. Build Home
First, the routing code is organized, and the top two lines of the route are routed:
Copy the Code code as follows:
Route::get ('/', ' welcomecontroller@index ');
Route::get (' Home ', ' homecontroller@index ');
Change to:
Copy the Code code as follows:
Route::get ('/', ' homecontroller@index ');
We will use HomeController directly to support our front page display.
You can now delete the learnlaravel5/app/http/controllers/welcomecontroller.php controller files and learnlaravel5/resources/views/ welcome.blade.php view file.
Modify the learnlaravel5/app/http/controllers/homecontroller.php to:
<?php namespace App\http\controllers;use app\page;class HomeController extends Controller {public Function index () {R Eturn view (' Home ')->withpages (Page::all ()); }}
The controller construction is complete.
' View (' home ')->withpages (Page::all ()) ' This sentence implements the following functions:
Render learnlaravel5/resources/views/home.blade.php View Files
Pass the variable $pages into view, $pages = Page::all ()
Page::all () invokes the All () method in eloquent, which returns all data in the pages table.
Next we start writing the view file:
First, we will create a unified shell of the front page, which is the "part and the #footer" section. Create a new learnlaravel5/resources/views/_layouts/default.blade.php file (the folder you created yourself):
Learn Laravel 5
@yield (' content ') ©2015 Johnlui
Modify the learnlaravel5/resources/views/home.blade.php file to:
@extends (' _layouts.default ') @section (' content ') Learn Laravel 5
{{inspiring::quote ()}}
@foreach ($pages as $page)
- ID)}} ">
{{$page->title}}
{{$page->body}}
@endforeach
@endsection
The first line ' @extends (' _layouts.default ') ' represents this page as a learnlaravel5/resources/views/_layouts/default.blade.php child view. At this point, the Laravel view rendering system loads the parent view first, and then puts the contents of the @section (' content ') in this view into the @yield (' content ') in the parent view.
To access http://localhost:88/, you can get the following page:
2. Building page Display page
First, increase the routing. Add a line below the first line of the route file:
Copy the Code code as follows:
Route::get (' Pages/{id} ', ' pagescontroller@show ');
New controller learnlaravel5/app/http/controllers/pagescontroller.php, responsible for the presentation of a single page:
<?php namespace App\http\controllers;use app\page;class Pagescontroller extends Controller {public function show ($id { return view (' Pages.show ')->withpage (Page::find ($id));}}
New View learnlaravel5/resources/views/pages/show.blade.php file:
{{$page->title}}
{{$page->updated_at}} {{$page->body}}
@endsection
Complete, test results: Click the title of any article in the first page, enter the article display page, you will see the following page:
At this point, the front desk display page is complete, the tutorial three ends.
The above mentioned is the whole content of this article, I hope to be able to learn LARAVEL5 framework to help you.
http://www.bkjia.com/PHPjc/981347.html www.bkjia.com true http://www.bkjia.com/PHPjc/981347.html techarticle Laravel 5 Framework Primer (iii), LARAVEL framework primer In this tutorial, we will use the Laravel 5 out-of-the-box Auth system to authenticate our background and build ...