我在我的AppServiceProvider中创建了这个指令,这是代码
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\DB;
use Blade;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Blade::directive('detect', function () {
if(!empty($_SERVER['HTTP_USER_AGENT'])){
$user_ag = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/(Mobile|Android|Tablet|GoBrowser|[0-9]x[0-9]*|uZardWeb\/|Mini|Doris\/|Skyfire\/|iPhone|Fennec\/|Maemo|Iris\/|CLDC\-|Mobi\/)/uis',$user_ag)){
return true;
};
};
return false;
});
}
}我现在想像这样在我的刀片文件中使用它
<div class="mb-3
@if(detect)
col-6
@else
col-3
@endif
expand_on_mobile">
<label for="exampleInputEmail1" class="form-label">Category</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>如果视图是在移动平台上提供的,我希望显示类col-6,但如果在桌面上显示它,则显示col-3。
到目前为止,我得到了错误undefined constant detect。
我怎么才能解决这个问题?
发布于 2022-10-12 15:39:19
我把它修好了。在我的AppServiceProvider.php里
Blade::if('detect', function ($value) {
if(!empty($_SERVER['HTTP_USER_AGENT'])){
$user_ag = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/(Mobile|Android|Tablet|GoBrowser|[0-9]x[0-9]*|uZardWeb\/|Mini|Doris\/|Skyfire\/|iPhone|Fennec\/|Maemo|Iris\/|CLDC\-|Mobi\/)/uis',$user_ag)){
return 'mobile';
};
}else{
return 'desktop';
}
});我正在用这个方法
<div
@detect("mobile")
class="mb-3 col-6 "
@else
class="mb-3 col-3 "
@enddetect
>
<label for="exampleInputEmail1" class="form-label">Category</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
</div>发布于 2022-10-12 14:56:00
您不需要使引导程序已经处理了媒体声明的过程变得过于复杂:)查看这个链接,以帮助您所需要做的就是在达到比tablet 引导文档到列响应性更高的位置时,为移动或col 6添加col 3。
https://stackoverflow.com/questions/74043862
复制相似问题