如何在 Laravel 中渲染 Web 和移动视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28030120/
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
How to render web and mobile views in Laravel
提问by horse
I have two blade files for each route, one for web and one for mobile devices. I don't know proper way to handle requests. Is that a proper way:
我为每条路线有两个刀片文件,一个用于网络,一个用于移动设备。我不知道处理请求的正确方法。这是正确的方法吗:
At the end of each controller function (For each request)
在每个控制器功能结束时(对于每个请求)
If it is mobile (via Jenssegers)
View::make(file_mobile.blade.php)
else
View::make(file_web.blade.php)
What would you suggest?
你有什么建议?
回答by jszobody
One option would be to use a library like Laravel Agent.
一种选择是使用像 Laravel Agent 这样的库。
https://github.com/jenssegers/Laravel-Agent
https://github.com/jenssegers/Laravel-Agent
if ( Agent::isMobile() ) {
View::make("file_mobile.blade.php");
} else {
View::make("file_web.blade.php");
}
Rather than repeat this in each controller method, you may want to abstract this out. A response macroseems like a good option, maybe something like:
您可能希望将其抽象出来,而不是在每个控制器方法中重复此操作。一个响应宏似乎是一个不错的选择,也许是这样的:
Response::macro('ress', function($viewname)
{
if ( Agent::isMobile() ) {
return View::make($viewname . "_mobile.blade.php");
} else {
return View::make($viewname . "_web.blade.php");
}
});
So that you can call this in your controller:
这样您就可以在控制器中调用它:
return Response::ress('file');
This is all untested code, just to point you in the direction of one possible solution.
这都是未经测试的代码,只是为了给您指明一种可能的解决方案的方向。
回答by user1669496
jszobody's answer is probably best since you already have the views for each version made, but in the future, I would consider controller layouts.
jszobody 的答案可能是最好的,因为您已经拥有每个版本的视图,但在未来,我会考虑控制器布局。
Basically what you would do is build two layouts, one for mobile and one for non-mobile and set them in the constructor of BaseController
. These layouts would contain all the necessary styling, navbar or whatever else all your views should have in common.
基本上你要做的是构建两种布局,一种用于移动设备,一种用于非移动设备,并将它们设置在BaseController
. 这些布局将包含所有必要的样式、导航栏或所有视图应该具有的任何其他内容。
public function __construct()
{
$this->layout = Agent::isMobile() ? 'layouts.mobile' : 'layouts.nonMobile';
}
Both layouts would have a @yields('content')
to give it a content section and all your views should only be worried about the content that shows in the layouts.
两种布局都有一个@yields('content')
给它一个内容部分,你的所有视图应该只关心布局中显示的内容。
Then all you have to do is instead of returning a view in your controllers, simply set the content section in the layout.
然后你所要做的就是不要在控制器中返回一个视图,只需在布局中设置内容部分。
$this->layout->content = View::make('user.content');`
This is what I do on my personal projects and it usually works out quite well. In the event you want to experiment with a new site layout or need to add a mobile layout or even an admin layout, simply create the layout, modify BaseController::__constructor()
to set it when you need to, and you are done.
这就是我在个人项目中所做的,通常效果很好。如果您想尝试新的站点布局或需要添加移动布局甚至管理布局,只需创建布局,BaseController::__constructor()
在需要时进行修改以进行设置,就大功告成了。