Home Backend Development PHP Tutorial Laravel 5 Basics (2) - Introduction to Routing, Controllers and Views

Laravel 5 Basics (2) - Introduction to Routing, Controllers and Views

Aug 08, 2016 am 09:26 AM
return

  • View app/Http/routes.php
<code>Route::get('/', 'WelcomeController@index');</code>
Copy after login

@ 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>
Copy after login

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>
Copy after login
<code>在浏览器中测试,得到一个简单的反馈。</code>
Copy after login
  • We create a new route and add:
  • in routes.php
<code>Route::get('/contact', 'WelcomeController@contact');</code>
Copy after login

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>
Copy after login
<code>在浏览器终测试新增加的路由。</code>
Copy after login
  • 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, just return 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>
Copy after login
  • Create the pages directory under the views directory, and then create contact.blade.php
<code>&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Contact&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;</code>
Copy after login

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.

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 Article Tags

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)

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

Detailed explanation of the usage of return in C language

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

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

Detailed explanation of JavaScript function return values ​​and return statements Detailed explanation of JavaScript function return values ​​and return statements Aug 04, 2022 am 09:46 AM

Detailed explanation of JavaScript function return values ​​and return statements

How to use return value in Python How to use return value in Python Oct 07, 2023 am 11:10 AM

How to use return value in Python

How does Vue3 use setup syntax sugar to refuse to write return How does Vue3 use setup syntax sugar to refuse to write return May 12, 2023 pm 06:34 PM

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

Use the return keyword in JavaScript Use the return keyword in JavaScript Feb 18, 2024 pm 12:45 PM

Use the return keyword in JavaScript

In Java, after the return statement in a method is executed, will the finally block be executed? In Java, after the return statement in a method is executed, will the finally block be executed? Sep 17, 2023 pm 03:05 PM

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

How to use return statement in JavaScript How to use return statement in JavaScript Feb 26, 2024 am 09:21 AM

How to use return statement in JavaScript

See all articles