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
laravel - home route
提问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.php
and home.blade.php
. That makes sense to me, but now how do I tell Laravel, or how do I route
to home.blade.php
?
所以我创建了我的模板,layout.blade.php
并且home.blade.php
. 这对我来说很有意义,但是现在我如何告诉Laravel,或者我怎么route
给home.blade.php
?
I'm looking for an explanation rather then just code. I'm trying to learn.
我正在寻找解释,而不仅仅是代码。我正在尝试学习。
回答by The Alpha
Actually, a view
in MVC
application is just a part of the application and it's only for presentation logic, the UI
and 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/function
and from there you show/load the view
. So
it's not a tutorial site and it's also not possible to explain about MVC
here, 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/class
or an anonymous function
in your apps/routes.php
file and show the view
from one of those. Just follow the given instruction step by step.
如果是Laravel
,您应该在文件中创建 acontroller/class
或 an并显示其中之一。只需一步一步地按照给定的说明进行操作。anonymous function
apps/routes.php
view
Using a Class:
使用类:
To create a routeto your Home
Controller 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 HomeController
controller/class (create a file in your controllers folder and save this file using HomeController.php
as 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.php
then it'll print Home Page
. So, to use a view you need a controller
or an anonymous function
and 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/class
to show the view
from directly your route, i.e.
此外,您可以使用匿名函数而不是 acontroller/class
来view
直接显示您的路线,即
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, Laravel
will call the anonymous function given in/as route's callback
and 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.php
file
另外,请记住,您在home.blade.php
文件的第一行有以下内容
@extends('layouts.layout')
It looks confusing, you may rename the main layout (layout.blade.php
) to master.blade.php
and use following in your home.blade.php
instead
它看起来令人困惑,您可以将主布局 ( 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 blade
templating) and also read some MVC examples, that may help you too understand the basics of an MVC
framework (you may find more by googling) and some good posts about MVC
on 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