Laravel:在路由上调用视图 vs 控制器

Laravel: calling the View on the Routes vs Controller

I want to know what are the differences between using Routes and Controller? Is there a major difference in Laravel's system, or is it just a matter of logic?

路由是您声明应用程序 URL 的地方,控制器包含表示应用程序逻辑的代码。

您可以将代码放在路由声明的回调参数中

Route::get('/user', function () {
    $user = User::where('active', '=', 1)
        ->where('parent_user_id', '=', Auth::id())
        ->paginate(10);
    return view('users', ['users' => $user]);
});

但是很快就会变得很乱,所以最好声明一个控制器相关的路由class

Route::get('/user', [UserController::class, 'index']);

Is there a difference between calling the View on the Routes and the View on the Controller?Does the Controller automatically cache the View and Routes does not?Because I read somewhere that if we call the View in the Controller, it will be cached, but if we call the View in the Routes, it is not so we have to cache the View manually in the Routes. Is this true?

没有区别,除非与第三方缓存包相关,否则两者都不缓存。

Does the Controller automatically cache the View and Routes does not? Because I read somewhere that if we call the View in the Controller, it will be cached, but if we call the View in the Routes, it is not so we have to cache the View manually in the Routes. Is this true?

视图和路由由 Laravel 自动缓存。

视图缓存存在于:storage/framework/views

如果您想管理视图的缓存:

php artisan view:clear
php artisan view:cache

路由缓存存在于:bootstrap/cache/routes.php

如果你想管理路由的缓存:

php artisan route:clear
php artisan route:cache