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

WBOY
Release: 2016-08-08 09:26:56
Original
1134 people have browsed it
  • 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><!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<h1>Contact</h1>
</body>
</html></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.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template