Laravel 5框架學習之向視圖傳送數(shù)據,laravel框架
Jun 13, 2016 am 09:07 AMLaravel 5框架學習之向視圖傳送數(shù)據,laravel框架
我們在Routes.php中新建一個路由
復制代碼 代碼如下:
Route::get('about', 'PagesController@about');
在瀏覽器中瀏覽會獲得一個錯誤,錯誤信息僅僅是一個提示信息,缺少細節(jié),在生產環(huán)境 It' ok,但是開發(fā)階段我們希望獲得詳細信息。
在項目的根目錄找到 .env 文件,修改
復制代碼 代碼如下:
APP_DEBUG=true
這將顯示詳細的錯誤信息,PagesController 不存在。但在生產環(huán)境一定要設置為 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,所有的方法我們需要自己創(chuàng)建。
如果你想知道到底有什么參數(shù)我們可以在命令行執(zhí)行,你可以運行下面的命令來查看幫助
復制代碼 代碼如下:
php artisan help make:controller
ok, 你可以經常使用help命令來幫助你了解這些參數(shù)。
在PagesController中建立about方法。
復制代碼 代碼如下:
?public function about() {
??????? return 'About Page';
??? }
在瀏覽器沖查看結果,錯誤消失,返回簡單的信息。
返回視圖
我們當然希望返回html文檔,修改about方法的返回:
復制代碼 代碼如下:
?public function about() {
??????? return view('pages.about');
??? }
注意:返回的結果是 pages.about ,這表示在 views 子目錄中的 pages 子目錄中的 about.balde.php 文件。讓我們創(chuàng)建 resources\views\pages\about.balde.php 文件
復制代碼 代碼如下:
???
???
About
That's it. 運行瀏覽器查看吧,??
向視圖傳送數(shù)據
修改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的語義的,讓我來修改一個數(shù)據:
復制代碼 代碼如下:
$name = 'Zhang Jinlgin';
查看結果,發(fā)現(xiàn)所有的html元素都被轉義了。但是如果不需要轉義html,可以使用 {!! !!},修改視圖:
復制代碼 代碼如下:
About {!! $name !!}
再看結果,??
以上所述就是本文的全部內容了,希望能夠對大家熟練掌握Laravel5有所幫助。

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)

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

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.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

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

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.
