Laravel 5 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29510055/
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 5 Routing
提问by Tartar
As a beginner at Laravel 5 programming i am trying to develop a basic application in order to learn Laravel 5 fundamentals. In my application, i have a couple of pages and i am trying to route them as:
作为 Laravel 5 编程的初学者,我正在尝试开发一个基本应用程序以学习 Laravel 5 基础知识。在我的应用程序中,我有几个页面,我试图将它们路由为:
Route::get('/', 'PagesController@index');
Route::get('/vehicles', 'PagesController@vehiclepage');
Route::get('/calculation', 'PagesController@calculationpage');
Route::get('/settings', 'PagesController@settingspage');
PagesController:
页面控制器:
class PagesController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return view('index');
}
public function vehiclepage()
{
return view('vehiclepage');
}
public function calculationpage()
{
return view('calculationpage');
}
public function settingspage()
{
return view('settingspage');
}
}
Some form process will be goin on in these pages ass well. I wonder, is there a more efficient way to have a better routing for my pages ? Any help would be appreciated.
这些页面中也会进行一些表单处理。我想知道,有没有更有效的方法来为我的页面提供更好的路由?任何帮助,将不胜感激。
回答by Svetoslav
1st of all if you want to access those pages at main route at your site as : /{pagename} the best way is to stay as you have it.. list them all 1 by 1. Its not good to have dynamic matching router for the main site route "/".
首先,如果您想在您的站点的主路由上访问这些页面:/{pagename} 最好的方法是保持原样.. 一一列出它们。拥有动态匹配路由器并不好主站点路由“/”。
If its ok for you to set some prefix to this route as: '/page/{pagename}' here is a nice dynamic loader..
如果您可以为此路由设置一些前缀为:'/page/{pagename}' 这里是一个不错的动态加载器..
class PagesController extends Controller {
public function show( $slug)
{
$pageslug = 'page.' . (string)$slug;
// This means that your views must be in views/page/ folder
if( view()->exists($pageslug)){
return view($pageslug);
}
abort(404);
}
}
And your route will be:
你的路线将是:
Route::get('/page/{slug}', 'PagesController@show');
At end all views in views/page/XXXX will be accessible at /page/{viewname}
最后,views/page/XXXX 中的所有视图都可以在 /page/{viewname} 访问
回答by kalatabe
Your routing is perfectly fine, although there are alternatives. For example, you may define your routing like so:
尽管有其他选择,但您的路由非常好。例如,您可以像这样定义路由:
routes.php
路由文件
Route::get('login', ['uses' => 'LoginController@index']);
Route::controller('/', 'WelcomeController');
WelcomeController.php
欢迎控制器.php
class PagesController extends Controller {
public function getIndex()
{
return view('homepage');
}
public function getVehicle()
{
return view('vehiclepage');
}
public function getCalculation()
{
return view('calculationpage');
}
public function getSettings()
{
return view('settingspage');
}
}
What this will do is route GET requests to your.domain/login to the index
method of your LoginController
(assuming you will have one), and if the parameter is anything else, it will try to find a method that starts with get
and ends with the word being requested, in the PagesController
, then serve it, so a GET request to your.domain/settings will end up in the getSettings
method of PagesController
. You can read more about how this all works in the docs
这将做的是将 GET 请求路由到 your.domain/login 到index
你的方法LoginController
(假设你有一个),如果参数是其他任何东西,它会尝试找到一个get
以单词开头和结尾的方法被请求,在PagesController
,则服务于它,这样一GET请求your.domain /设置将在最后getSettings
的方法PagesController
。您可以在文档中阅读有关这一切如何运作的更多信息
Note - I'm not saying that this method is better- some people say that "monolithic" routers are the Devil, while others (like me) prefer to have all the logic in the controller and have the routing defined in the most condensed way possible. It all comes down to personal preference and style. Just keep in mind that consistency is key - whichever approach you prefer, stick to it!
注意 - 我并不是说这种方法更好- 有些人说“单体”路由器是魔鬼,而其他人(像我一样)更喜欢在控制器中拥有所有逻辑并以最简洁的方式定义路由可能的。这一切都归结为个人喜好和风格。请记住,一致性是关键——无论您喜欢哪种方法,都要坚持下去!
回答by EntGriff
I had same question, because I was coming from KOHANA FW and then I did in my Laravel project like this :
我有同样的问题,因为我来自 KOHANA FW,然后我在我的 Laravel 项目中这样做了:
Route::get('/{controller?}/{action?}/{id?}', function ($controller='Home', $action='defaulmethod', $id = null) {
$controller = ucfirst($controller);
return APP::make("{$controller}Controller")->$action($id);
});
It worked when url was like this :
当 url 是这样的时候它起作用了:
/{Controller}/{action}/{id}
but i think your route system is not bad, it is very easy to understand and if you don't make big project - you can use it.
但我认为你的路线系统还不错,它很容易理解,如果你不做大项目 - 你可以使用它。
回答by Kornkrit Leelhaphunt
Try this
尝试这个
Route::get('{page}', function($page) {
return View::make($page);
});
回答by bbparis
I'm new to laravel also, I think you made the things in the right way. But just a small advice, try to keep your code very clean for your future use, like that :
我也是 laravel 的新手,我认为您以正确的方式制作了这些东西。但只是一个小建议,尽量让你的代码非常干净以备将来使用,就像这样:
class PagesController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return view('index');
}
public function vehiclepage()
{
return view('vehiclepage');
}
public function calculationpage()
{
return view('calculationpage');
}
public function settingspage()
{
return view('settingspage');
}
}
As the public function vehiclepage is new function and it is not a part of the public function index.
由于公共功能车辆页面是新功能,不属于公共功能索引的一部分。
回答by WahidSherief
you can do route-model binding (google it) that you can ignore using wildcards such as
你可以做路由模型绑定(谷歌它),你可以使用通配符忽略它,例如
pagename/{id}
you can use Route::resource()
see: http://laravel.com/docs/5.0/controllers#restful-resource-controllers
你可以使用Route::resource()
看到:http: //laravel.com/docs/5.0/controllers#restful-resource-controllers
回答by Muhammad Sadiq
The implicit controller routes may be fine for you if u wish,as said by Kaloyan Doichinov. your route.php file will be like Route::controller('/','yourControllerName');
正如 Kaloyan Doichinov 所说,如果您愿意,隐式控制器路由可能适合您。您的 route.php 文件将类似于 Route::controller('/','yourControllerName');
and ur controller method should be prepand with http verb,like eg index() => getIndex(){} etc
并且你的控制器方法应该在前面加上 http 动词,例如 index() => getIndex(){} 等
so for post request use => postMethodName(){} //change MethodName with ur method name.
所以对于发布请求使用 => postMethodName(){} //用你的方法名称更改 MethodName。
and so on for put,delete etc requests.......
等等用于放置,删除等请求......