如果路徑中包含 PHP Laravel 中的特定單詞,我如何在路徑中搜索我想在當前頁面的標題中找到單詞(學生),以便我可以放置同義詞或翻譯我的代碼......
if ( Route::currentRouteName() .... 在這里寫什么 .... 'students' )
我有一個代碼將在此處執行,以防找到單詞 (students)。
萬一
我有這些鏈接...學生/索引學生/創建學生/編輯...
如果它在鏈接中,我想搜索單詞 (students)
uj5u.com熱心網友回復:
這是另一個在我看來效率更高的解決方案,因為它使用了 Laravel 的內置方法。
在您的示例中,如果您想檢查您的路線名稱,您可以使用 Laravel 幫助程式:
use Illuminate\Support\Str;
if(Str::is('students/*', Route::current()->getName())) {
//
}
檔案:https ://laravel.com/docs/9.x/helpers#method-str-is
現在,要替代@Manuel Glez 的解決方案,您可以使用模式直接檢查請求的路徑/路由:
if ($request->is('students/*')) {
//
}
檔案:https ://laravel.com/docs/9.x/requests#inspecting-the-request-path
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490513.html
