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

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

WBOY
WBOYOriginal
2016-08-08 09:26:561115browse
  • 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, 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>
  • 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.

Statement:
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