search
HomeBackend DevelopmentPHP TutorialGetting Started with Laravel 5 Framework (3), Getting Started with Laravel Framework_PHP Tutorial

Getting started with the Laravel 5 framework (3), getting started with the laravel framework

In this tutorial, we will use the out-of-the-box Auth system that comes with Laravel 5 to run our backend Perform permission verification and build a front-end page to display Pages.

1. Permission verification

The backend address is http://localhost:88/admin, and all our backend operations will be performed under this page or its subpages. Using the Auth provided by Laravel 5, we only need to change a small part of the routing code to implement the permission verification function.

First, change the code of the routing group to:

Copy code The code is 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: adding `'middleware' => 'auth'` to the first parameter (an array) of `Route::group()`. Now visit http://localhost:88/admin and you should be redirected to the login page. If there is no jump, don't panic, just exit from the upper right corner and re-enter.

Our personal blog system does not allow people to register casually. Below we will change part of the routing code and only retain the basic login and logout functions.

Delete:

Copy code The code is as follows:
Route::controllers([
'auth' => 'AuthAuthController',
'password' => 'AuthPasswordController',
]);

Added:

Copy code The code is as follows:
Route::get('auth/login', 'AuthAuthController@getLogin');
Route::post('auth/login', 'AuthAuthController@postLogin');
Route::get('auth/logout', 'AuthAuthController@getLogout');

The backend with the minimization function of permission verification has been completed. This backend currently only manages the Page resource. Next we will build the front page and display Pages.

2. Build the homepage

First organize the routing code and change the top two lines of the routing:

Copy code The code is as follows:
Route::get('/', 'WelcomeController@index');
Route::get('home', 'HomeController@index');

Change to:

Copy code The code is as follows:
Route::get('/', 'HomeController@index');

We will use HomeController directly to support our front page display.

You can delete the learnlaravel5/app/Http/Controllers/WelcomeController.php controller file and learnlaravel5/resources/views/welcome.blade.php view file at this time.

Modify learnlaravel5/app/Http/Controllers/HomeController.php to:

<&#63;php namespace App\Http\Controllers;

use App\Page;

class HomeController extends Controller {

 public function index()
 {
 return view('home')->withPages(Page::all());
 }

}

The controller construction is completed.

`view('home')->withPages(Page::all())` This sentence implements the following functions:

Render learnlaravel5/resources/views/home.blade.php view file
Pass the variable $pages into the view, $pages = Page::all()
Page::all() calls the all() method in Eloquent and returns all the data in the pages table.
Next we start writing the view file:

First, we will create a unified shell of the front-end page, namely the `

` part and the `#footer` part. Create a new learnlaravel5/resources/views/_layouts/default.blade.php file (please create the folder yourself):
<!DOCTYPE html>
<html lang="zh-CN">
<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>Learn Laravel 5</title>

 <link href="/css/app.css" rel="stylesheet">

 <!-- Fonts -->
 <link href='http://fonts.useso.com/css&#63;family=Roboto:400,300' rel='stylesheet' type='text/css'>
</head>
<body>

 <div class="container" style="margin-top: 20px;">
  @yield('content')
  <div id="footer" style="text-align: center; border-top: dashed 3px #eeeeee; margin: 50px 0; padding: 20px;">
   &copy;2015 <a href="http://lvwenhan.com">JohnLui</a>
  </div>
 </div>


</body>
</html>

Modify the learnlaravel5/resources/views/home.blade.php file to:

@extends('_layouts.default')

@section('content')
 <div id="title" style="text-align: center;">
 <h1 id="Learn-Laravel">Learn Laravel 5</h1>
 <div style="padding: 5px; font-size: 16px;">{{ Inspiring::quote() }}</div>
 </div>
 <hr>
 <div id="content">
 <ul>
  @foreach ($pages as $page)
  <li style="margin: 50px 0;">
  <div class="title">
   <a href="{{ URL('pages/'.$page->id) }}">
   <h4 id="page-title">{{ $page->title }}</h4>
   </a>
  </div>
  <div class="body">
   <p>{{ $page->body }}</p>
  </div>
  </li>
  @endforeach
 </ul>
 </div>
@endsection

The first line `@extends('_layouts.default')` means that this page is a subview of learnlaravel5/resources/views/_layouts/default.blade.php. At this time, Laravel's view rendering system will first load the parent view, and then put the content in @section('content') in this view into @yield('content') in the parent view for rendering.

Visit http://localhost:88/ and you will get the following page:

2. Build Page display page

First add routing. Add a line below the first line of the routing file:

Copy code The code is as follows:
Route::get('pages/{id}', 'PagesController@show');

Create a new controller learnlaravel5/app/Http/Controllers/PagesController.php, responsible for the display of a single page:

<&#63;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:

@extends('_layouts.default')

@section('content')
 <h4>
  <a href="/">&#11013;&#65039;返回首页</a>
 </h4>

 <h1 id="page-title">{{ $page->title }}</h1>
 <hr>
 <div id="date" style="text-align: right;">
  {{ $page->updated_at }}
 </div>
 <div id="content" style="padding: 50px;">
  <p>
   {{ $page->body }}
  </p>
 </div>
@endsection

All completed, check the results: click on the title of any article on the homepage to enter the article display page, you will see the following page:

At this point, the front-end display page is completed, and tutorial three is over.

The above is the entire content of this article. I hope it will be helpful to everyone learning the Laravel5 framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/981347.htmlTechArticleGetting Started with Laravel 5 Framework (3), Getting Started with Laravel Framework In this tutorial, we will use Laravel 5’s own The out-of-the-box Auth system performs permission verification on our backend and builds...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software