laravel 函數測試 --- Route::has()


新建: D:\laragon\www\test\resources\views\t.blade.php

    @if (Route::has('login'))
        <div class="top-right links">
            @auth
                <a href="{{ url('/home') }}">Home</a>
            @else
                <a href="{{ route('login') }}">Login</a>

            @endauth
        </div>
    @else
        <h3>沒有定義login這個路由, 沒辦法這樣訪問  {{ url('/login') }}</h3>
   
    @endif

加上@符號,雙{}將不解析<br>
Hello, @{{ name }} .

{{ isset($name) ? $name : 'Default' }}
除了使用三元運算符,Blade 還提供了更簡單的方式:

{{ $name or 'Default' }}

新建:
D:\laragon\www\test\routes\web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/t', function () {
    return view('t');
});

Route::get('/login', function () {
    return view('t');
})->name('login');

//路由加參數:在/t.blade.php中可以使用變量 {{$name}}
Route::get('/tname', function () {
    return view('t', ['name' => 'shen']);
});

如果 去掉->name('login'); 則找不到路由,Route::has('login') 和name('login'); 要成對出現 。萬物有名。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM