在 Laravel 中创建一个新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43221317/
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
Creating a new page in Laravel
提问by Ben Kao
I'm trying to create a new page in Laravel and I'm not sure what to do in the context of the Laravel framework. If it was just html, then you just create a new html file. In Laravel, what are all the things you need to do when creating a new page?
我正在尝试在 Laravel 中创建一个新页面,但我不确定在 Laravel 框架的上下文中要做什么。如果它只是 html,那么您只需创建一个新的 html 文件。在 Laravel 中,创建新页面时需要做的所有事情是什么?
回答by Dwight
The easiest way in Laravel is to define a route closure, and return a view from it.
Laravel 中最简单的方法是定义一个路由闭包,并从它返回一个视图。
In your routes/web.php
file:
在您的routes/web.php
文件中:
Route::get('/my-page', function () {
return view('my-page');
});
Then in resources/views
you create a file called my-page.php
, or if you want to use Laravel's Blade syntax (you probably do) call it my-page.blade.php
.
然后在resources/views
您创建一个名为 的文件中my-page.php
,或者如果您想使用 Laravel 的 Blade 语法(您可能会这样做),请调用它my-page.blade.php
.
回答by Piotr Ficner
There are two locations to add New Page to your Laravel project:
有两个位置可以将新页面添加到 Laravel 项目中:
- You have to create an additional route in YOURAPP>routes>web.php file.
- You have to add PHP file with that name to YOURAPP>resources>views folder. If you want to use BLADE for your project, than you should put name_of_page.blade.php. And you should concider it as a must at the very beginning :).
- 您必须在 YOURAPP>routes>web.php 文件中创建一个额外的路由。
- 您必须将具有该名称的 PHP 文件添加到 YOURAPP>resources>views 文件夹中。如果你想在你的项目中使用 BLADE,那么你应该放置 name_of_page.blade.php。你应该在一开始就认为它是必须的:)。
Now, in routes you should be able to add only first part of the file rather than .blade.php. For example: about.blade.php, you can put in route only about
现在,在路由中,您应该只能添加文件的第一部分,而不是 .blade.php。例如:about.blade.php,你可以只在路由中放入about
It should work out of the box :).
它应该是开箱即用的:)。