laravel - 回家路线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19335660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:33:16  来源:igfitidea点击:

laravel - home route

phplaravel

提问by devs

I'm learning Laravel, and for my first project I'd like to create my portfolio. However, the first task I have to do is confusing me.

我正在学习Laravel,对于我的第一个项目,我想创建我的投资组合。然而,我必须做的第一项任务是让我感到困惑。

So I created my templates, layout.blade.phpand home.blade.php. That makes sense to me, but now how do I tell Laravel, or how do I routeto home.blade.php?

所以我创建了我的模板,layout.blade.php并且home.blade.php. 这对我来说很有意义,但是现在我如何告诉Laravel,或者我怎么routehome.blade.php

I'm looking for an explanation rather then just code. I'm trying to learn.

我正在寻找解释,而不仅仅是代码。我正在尝试学习。

回答by The Alpha

Actually, a viewin MVCapplication is just a part of the application and it's only for presentation logic, the UIand one doesn't call/load a view directly without the help of another part (controller/function) of the application. Basically, you make a request to a route and that route passes the control over to a controller/functionand from there you show/load the view. Soit's not a tutorial site and it's also not possible to explain about MVChere, you should read about it and for Laravel, it's best place to understand the basics on it's documentation, well explained with examples, anyways.

实际上,应用程序view中的MVC应用程序只是应用程序的一部分,它仅用于表示逻辑,UI并且在没有应用程序另一部分(控制器/功能)的帮助的情况下,不会直接调用/加载视图。基本上,您向路由发出请求,该路由将控制权传递给 acontroller/function并从那里显示/加载view. So它不是一个教程网站,也无法MVC在这里解释,你应该阅读它,因为Laravel它是理解它的文档基础知识的最佳场所,无论如何都用例子很好地解释了。

In case of Laravel, you should create a controller/classor an anonymous functionin your apps/routes.phpfile and show the viewfrom one of those. Just follow the given instruction step by step.

如果是Laravel,您应该在文件中创建 acontroller/class或 an并显示其中之一。只需一步一步地按照给定的说明进行操作。anonymous functionapps/routes.phpview

Using a Class:

使用类:

To create a routeto your HomeController you should add this code in your app/routes.php

要创建到您的控制器的路由,您Home应该将此代码添加到您的app/routes.php

// This will call "showWelcome" method in your "HomeController" class
Route::any('/', array( 'as' => 'home', 'uses' => 'HomeController@showWelcome' ));

Then create the HomeControllercontroller/class (create a file in your controllers folder and save this file using HomeController.phpas it's name) then paste the code given below

然后创建HomeController控制器/类(在你的控制器文件夹中创建一个文件并使用HomeController.php它的名称保存这个文件)然后粘贴下面给出的代码

class HomeController extends BaseController {
    public function showWelcome()
{
        // whatever you do, do it here
        // prepare some data to use in the view (optional)
        $data['page_title'] = 'Home Page';
        // finally load the view
        return View::make('home', $data);
    }
}

If you have {{ $title }}in your home.blade.phpthen it'll print Home Page. So, to use a view you need a controlleror an anonymous functionand load the view from the controller/function.

如果你有{{ $title }}home.blade.php那么它会打印Home Page。因此,要使用视图,您需要 acontroller或 ananonymous function并从控制器/函数加载视图。

Using an anonymous function:

使用匿名函数:

Also, you can use an anonymous function instead of a controller/classto show the viewfrom directly your route, i.e.

此外,您可以使用匿名函数而不是 acontroller/classview直接显示您的路线,即

Route::any('/', function(){
    // return View::make('home'); 
    // or this
    $data['page_title'] = 'Home Page'; // (optional)
    return View::make('home', $data);
});

Using this approach, whenever you make a request to the home page, Laravelwill call the anonymous function given in/as route's callbackand from there you show your view.

使用这种方法,每当您向主页发出请求时,Laravel都会调用在/作为路由中给出的匿名函数,callback并从那里显示您的视图。

Make sure to extend the the master/main layout in sub view (home):

确保在子视图(主页)中扩展主/主布局:

Also, remember that, you have following at the first line of your home.blade.phpfile

另外,请记住,您在home.blade.php文件的第一行有以下内容

@extends('layouts.layout')

It looks confusing, you may rename the main layout (layout.blade.php) to master.blade.phpand use following in your home.blade.phpinstead

它看起来令人困惑,您可以将主布局 ( layout.blade.php)重命名为master.blade.php并在您的home.blade.php替代中使用以下内容

@extends('layouts.master')

Read the doc/understand basics:

阅读文档/了解基础知识:

You should read Laravel's documentationproperly, (check templatesto understand bladetemplating) and also read some MVC examples, that may help you too understand the basics of an MVCframework (you may find more by googling) and some good posts about MVCon SO.

您应该正确阅读Laravel 的文档(检查模板以了解blade模板)并阅读一些MVC 示例,这可能有助于您了解MVC框架的基础知识(您可以通过谷歌搜索找到更多信息)以及一些MVC关于SO 的好帖子。

回答by RDK

Check it routing in Laravel.

在 Laravel 中检查它的路由

You need to use route file and controllers

您需要使用路由文件和控制器

Create needed function in your Controller file and create a template file for example

在您的控制器文件中创建所需的功能并创建一个模板文件,例如

class UserController extends BaseController {

    /**
     * Show the profile for the given user.
     */
    public function showProfile($id)
    {
        $user = User::find($id);

        return View::make('user.profile', array('user' => $user));
    }

}

you need to create view file views/user/profile.blade.php

你需要创建视图文件 views/user/profile.blade.php

View::make('user.profile', array('user' => $user)) == views/user/profile.blade.php

And you should read it http://laravel.com/docs/responsesand also this http://laravel.com/docs/quick#creating-a-view

你应该阅读它http://laravel.com/docs/responses以及这个http://laravel.com/docs/quick#creating-a-view