Table of Contents
Getting started with the Laravel 5 framework (3), getting started with the laravel framework
Home Backend Development PHP Tutorial Getting Started with Laravel 5 Framework (3), Getting Started with Laravel Framework_PHP Tutorial

Getting Started with Laravel 5 Framework (3), Getting Started with Laravel Framework_PHP Tutorial

Jul 13, 2016 am 09:57 AM
php framework

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());
 }

}

Copy after login

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>

Copy after login

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

@extends('_layouts.default')

@section('content')
 <div id="title" style="text-align: center;">
 <h1>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>{{ $page->title }}</h4>
   </a>
  </div>
  <div class="body">
   <p>{{ $page->body }}</p>
  </div>
  </li>
  @endforeach
 </ul>
 </div>
@endsection

Copy after login

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));
 }

}

Copy after login

New view learnlaravel5/resources/views/pages/show.blade.php file:

@extends('_layouts.default')

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

 <h1 style="text-align: center; margin-top: 50px;">{{ $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

Copy after login

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 of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Comparison of the advantages and disadvantages of PHP frameworks: Which one is better? Comparison of the advantages and disadvantages of PHP frameworks: Which one is better? Jun 04, 2024 pm 03:36 PM

The choice of PHP framework depends on project needs and developer skills: Laravel: rich in features and active community, but has a steep learning curve and high performance overhead. CodeIgniter: lightweight and easy to extend, but has limited functionality and less documentation. Symfony: Modular, strong community, but complex, performance issues. ZendFramework: enterprise-grade, stable and reliable, but bulky and expensive to license. Slim: micro-framework, fast, but with limited functionality and a steep learning curve.

Performance differences of PHP frameworks in different development environments Performance differences of PHP frameworks in different development environments Jun 05, 2024 pm 08:57 PM

There are differences in the performance of PHP frameworks in different development environments. Development environments (such as local Apache servers) suffer from lower framework performance due to factors such as lower local server performance and debugging tools. In contrast, a production environment (such as a fully functional production server) with more powerful servers and optimized configurations allows the framework to perform significantly better.

PHP Frameworks and Microservices: Cloud Native Deployment and Containerization PHP Frameworks and Microservices: Cloud Native Deployment and Containerization Jun 04, 2024 pm 12:48 PM

Benefits of combining PHP framework with microservices: Scalability: Easily extend the application, add new features or handle more load. Flexibility: Microservices are deployed and maintained independently, making it easier to make changes and updates. High availability: The failure of one microservice does not affect other parts, ensuring higher availability. Practical case: Deploying microservices using Laravel and Kubernetes Steps: Create a Laravel project. Define microservice controllers. Create Dockerfile. Create a Kubernetes manifest. Deploy microservices. Test microservices.

Integration of PHP frameworks with DevOps: the future of automation and agility Integration of PHP frameworks with DevOps: the future of automation and agility Jun 05, 2024 pm 09:18 PM

Integrating PHP frameworks with DevOps can improve efficiency and agility: automate tedious tasks, free up personnel to focus on strategic tasks, shorten release cycles, accelerate time to market, improve code quality, reduce errors, enhance cross-functional team collaboration, and break down development and operations silos

The best PHP framework for microservice architecture: performance and efficiency The best PHP framework for microservice architecture: performance and efficiency Jun 03, 2024 pm 08:27 PM

Best PHP Microservices Framework: Symfony: Flexibility, performance and scalability, providing a suite of components for building microservices. Laravel: focuses on efficiency and testability, provides a clean API interface, and supports stateless services. Slim: minimalist, fast, provides a simple routing system and optional midbody builder, suitable for building high-performance APIs.

Which PHP framework offers the most comprehensive extension library for rapid development? Which PHP framework offers the most comprehensive extension library for rapid development? Jun 04, 2024 am 10:45 AM

The PHP framework extension library provides four frameworks for selection: Laravel: Known for its vast ecosystem and third-party packages, it provides authentication, routing, validation and other extensions. Symfony: Highly modular, extending functionality through reusable "Bundles", covering areas such as authentication and forms. CodeIgniter: lightweight and high-performance, providing practical extensions such as database connection and form validation. ZendFramework: Powerful enterprise-level features, with extensions such as authentication, database connection, RESTfulAPI support, etc.

The application potential of artificial intelligence in PHP framework The application potential of artificial intelligence in PHP framework Jun 03, 2024 am 11:01 AM

The application potential of Artificial Intelligence (AI) in PHP framework includes: Natural Language Processing (NLP): for analyzing text, identifying emotions and generating summaries. Image processing: used to identify image objects, face detection and resizing. Machine learning: for prediction, classification and clustering. Practical cases: chatbots, personalized recommendations, fraud detection. Integrating AI can enhance website or application functionality, providing powerful new features.

PHP Frameworks and Artificial Intelligence: A Developer's Guide PHP Frameworks and Artificial Intelligence: A Developer's Guide Jun 04, 2024 pm 12:47 PM

Use a PHP framework to integrate artificial intelligence (AI) to simplify the integration of AI in web applications. Recommended framework: Laravel: lightweight, efficient, and powerful. CodeIgniter: Simple and easy to use, suitable for small applications. ZendFramework: Enterprise-level framework with complete functions. AI integration method: Machine learning model: perform specific tasks. AIAPI: Provides pre-built functionality. AI library: handles AI tasks.

See all articles