gpt4 book ai didi

php - 如何在 Laravel 中呈现 Web 和移动 View

转载 作者:可可西里 更新时间:2023-11-01 00:08:51 25 4
gpt4 key购买 nike

我为每条路线准备了两个 Blade 文件,一个用于网络,一个用于移动设备。我不知道处理请求的正确方法。这是正确的方法吗:

在每个 Controller 函数的末尾(对于每个请求)

If it is mobile (via Jenssegers)
View::make(file_mobile.blade.php)
else
View::make(file_web.blade.php)

你有什么建议?

最佳答案

一种选择是使用像 Laravel Agent 这样的库。

https://github.com/jenssegers/Laravel-Agent

if ( Agent::isMobile() ) {
View::make("file_mobile.blade.php");
} else {
View::make("file_web.blade.php");
}

与其在每个 Controller 方法中重复这一点,不如将其抽象出来。 response macro似乎是个不错的选择,可能是这样的:

Response::macro('ress', function($viewname)
{
if ( Agent::isMobile() ) {
return View::make($viewname . "_mobile.blade.php");
} else {
return View::make($viewname . "_web.blade.php");
}
});

这样你就可以在你的 Controller 中调用它:

return Response::ress('file');

这些都是未经测试的代码,只是为了向您指出一种可能的解决方案。

关于php - 如何在 Laravel 中呈现 Web 和移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com