


Laravel 5 Basics (2) - Introduction to Routing, Controllers and Views
Aug 08, 2016 am 09:26 AM- View
app/Http/routes.php
<code>Route::get('/', 'WelcomeController@index');</code>
@ is a delimiter, preceded by the controller and followed by the action, which means that when the user requests url /, the index method in the controller WelcomeController is executed
app/http/controllers/welcomecontroller.php
<code>public function index() { return view('welcome'); }</code>
Currently, a view is returned by default. The name of the view is welcome, which is actually welcome.blade.php. Blade is the view template of laravel.
You can view `resources/views/welcome.blade.php
Modify welcomecontroller.php
<code>public function index() { // return view('welcome'); return 'hello, laravel'; }</code>
<code>在浏览器中测试,得到一个简单的反馈。</code>
- We create a new route and add: in routes.php
<code>Route::get('/contact', 'WelcomeController@contact');</code>
You can create a new route, but for now we still use the default controller directly and add:
in WelcomeController.php<code>public function contact() { return 'Contact Me'; }</code>
<code>在浏览器终测试新增加的路由。</code>
We can return a simple string, or a json or html file. All view files are stored in resource->views.
For example:return view('welcome')
, we don't need to consider the path, and don't add the .blade.php extension, the framework does it for us automatically. If you need a subdirectory in the views directory, such as the views/forum subdirectory, justreturn view('forum/xxx')
, or the simple and clear way is:return view('forum.xxx')
. ??We return to a page
<code>public function contact() { return view('pages.contact'); }</code>
- Create the pages directory under the views directory, and then create contact.blade.php
<code><!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>Contact</h1> </body> </html></code>
The above introduces the basics of Laravel 5 (2) - Introduction to routing, controllers and views, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

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

Detailed explanation of the usage of return in C language

What is the execution order of return and finally statements in Java?

Detailed explanation of JavaScript function return values and return statements

How does Vue3 use setup syntax sugar to refuse to write return

In Java, after the return statement in a method is executed, will the finally block be executed?

How to use return statement in JavaScript
