Laravel 5.2 检查视图是否存在的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37500118/
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.2 method to check is view exists?
提问by Dzikri Ulinnuha A. Zamroni
Is there any method to check if view exists?
有什么方法可以检查视图是否存在?
Like PHP file_exists, but using internal laravel method for convenience
像PHP file_exists,但为了方便使用内部laravel方法
i want to code like this:
我想这样编码:
if(__ifViewExist__($view)){
return view($view)->render();
}
return "Page tidak ditemukan";
回答by IllegalPigeon
Yes, an exists() method is available.
是的,可以使用 exists() 方法。
if(view()->exists($view)){
return view($view)->render();
}
return "Page tidak ditemukan";
回答by geckob
Yes. Using the View
facade.
是的。使用View
门面。
// Return true when welcome.blade.php does not exist in theview folder
View::exists('welcome');
// Return false when login.blade.php does not exist in the view folder
View::exists('login');
回答by Inzmam ul Haq
In web.php
, you need to type below given code:
在 中web.php
,您需要输入以下给定的代码:
Route::get('/Test',function(){
if(View::exists('View'))
{
return view('View.php',['name' => 'Your string']);
}
return "File not found";
});
回答by Jay Soni
try {
return view($view);
} catch (\Exception $e) {
return "Page tidak ditemukan";
}
will be more efficient way
将是更有效的方式