Laravel 5框架学习之向视图传送数据,laravel框架
Laravel 5框架学习之向视图传送数据,laravel框架
我们在Routes.php中新建一个路由
复制代码 代码如下:
Route::get('about', 'PagesController@about');
在浏览器中浏览会获得一个错误,错误信息仅仅是一个提示信息,缺少细节,在生产环境 It' ok,但是开发阶段我们希望获得详细信息。
在项目的根目录找到 .env 文件,修改
复制代码 代码如下:
APP_DEBUG=true
这将显示详细的错误信息,PagesController 不存在。但在生产环境一定要设置为 false
我们可以手工新建控制器,但更快的方式是利用 laravel 提供的生成器。在命令行当前项目目录中运行:
复制代码 代码如下:
php artisan
可以看到laravel提供的功能。
复制代码 代码如下:
php artisan make:controller PagesController
ok,在 app->http->controller 下面生成了 PagesController.php
<?php namespace App\Http\Controllers; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class PagesController 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. * * @return Response */ public function store() { // } /** * 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 int $id * @return Response */ public function update($id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // } }
这样生成的controller包含了全部所需要的RESTful方法,我们可以简化一下。删除生成的PagesController.php,在命令行运行:
复制代码 代码如下:
php artisan make:controller PagesController --plain
再看一下生成的结果
复制代码 代码如下:
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PagesController extends Controller {
//
}
基本上是一个空的controller,所有的方法我们需要自己创建。
如果你想知道到底有什么参数我们可以在命令行执行,你可以运行下面的命令来查看帮助
复制代码 代码如下:
php artisan help make:controller
ok, 你可以经常使用help命令来帮助你了解这些参数。
在PagesController中建立about方法。
复制代码 代码如下:
public function about() {
return 'About Page';
}
在浏览器冲查看结果,错误消失,返回简单的信息。
返回视图
我们当然希望返回html文档,修改about方法的返回:
复制代码 代码如下:
public function about() {
return view('pages.about');
}
注意:返回的结果是 pages.about ,这表示在 views 子目录中的 pages 子目录中的 about.balde.php 文件。让我们创建 resources\views\pages\about.balde.php 文件
复制代码 代码如下:
About
That's it. 运行浏览器查看吧,😄
向视图传送数据
修改PagesController.php
复制代码 代码如下:
public function about() {
$name = 'Zhang Jinlgin';
return view('pages.about')->with('name', $name);
}
修改我们的视图文件 about.blade.php
复制代码 代码如下:
About
Bingo,查看结果吧。
我们使用的laravel使用了blade模板,我们可以利用这个好处修改视图:
复制代码 代码如下:
About {{ $name }}
看起来更好了,在blade中,{{}}是转义html的语义的,让我来修改一个数据:
复制代码 代码如下:
$name = 'Zhang Jinlgin';
查看结果,发现所有的html元素都被转义了。但是如果不需要转义html,可以使用 {!! !!},修改视图:
复制代码 代码如下:
About {!! $name !!}
再看结果,👌
以上所述就是本文的全部内容了,希望能够对大家熟练掌握Laravel5有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Tables are an essential component in many web applications. Tables usually have large amounts of data, so tables require some specific features to improve user experience. One of the important features is editability. In this article, we will explore how to implement editable tables using Vue.js and provide specific code examples. Step 1: Prepare the data First, we need to prepare the data for the table. We can use a JSON object to store the table's data and store it in the data property of the Vue instance. In this case

Python web development framework comparison: DjangovsFlaskvsFastAPI Introduction: In Python, a popular programming language, there are many excellent web development frameworks to choose from. This article will focus on comparing three popular Python web frameworks: Django, Flask and FastAPI. By comparing their features, usage scenarios and code examples, it helps readers better choose the framework that suits their project needs. 1. Django

Compare SpringBoot and SpringMVC and understand their differences. With the continuous development of Java development, the Spring framework has become the first choice for many developers and enterprises. In the Spring ecosystem, SpringBoot and SpringMVC are two very important components. Although they are both based on the Spring framework, there are some differences in functions and usage. This article will focus on comparing SpringBoot and Spring

I guess that many students want to learn the typesetting skills of Word, but the editor secretly tells you that before learning the typesetting skills, you need to understand the word views clearly. In Word2007, 5 views are provided for users to choose. These 5 views include pages. View, reading layout view, web layout view, outline view and normal view, let’s learn about these 5 word views with the editor today. 1. Page view Page view can display the appearance of the print result of the Word2007 document, which mainly includes headers, footers, graphic objects, column settings, page margins and other elements. It is the page view closest to the print result. 2. Reading layout view Reading layout view displays Word2007 documents and Office in the column style of a book

In this series, we will discuss how to build web applications using WordPress. Although this is not a technical series where we will look at code, we cover topics such as frameworks, fundamentals, design patterns, architecture, and more. If you haven’t read the first article in the series, I recommend it; however, for the purposes of this article, we can summarize the previous article as follows: In short, software can be built on frameworks, software can Extend the base. Simply put, we distinguish between framework and foundation—two terms that are often used interchangeably in software, even though they are not the same thing. WordPress is a foundation because it is an application in itself. It's not a framework. For this reason, when it comes to WordPress

The advantages of C++ in web development include speed, performance, and low-level access, while limitations include a steep learning curve and memory management requirements. When choosing a web development language, developers should consider the advantages and limitations of C++ based on application needs.

To use C++ for web development, you need to use frameworks that support C++ web application development, such as Boost.ASIO, Beast, and cpp-netlib. In the development environment, you need to install a C++ compiler, text editor or IDE, and web framework. Create a web server, for example using Boost.ASIO. Handle user requests, including parsing HTTP requests, generating responses, and sending them back to the client. HTTP requests can be parsed using the Beast library. Finally, a simple web application can be developed, such as using the cpp-netlib library to create a REST API, implementing endpoints that handle HTTP GET and POST requests, and using J

C++ Web development requires mastering the basics of C++ programming, network protocols, and database knowledge. Necessary resources include web frameworks such as cppcms and Pistache, database connectors such as cppdb and pqxx, and auxiliary tools such as CMake, g++, and Wireshark. By learning practical cases, such as creating a simple HTTP server, you can start your C++ Web development journey.
