laravel 语法 '::' at view 函数意义

bat*_*hir 5 laravel laravel-5 laravel-views

<?php
namespace Laravel\Horizon\Http\Controllers;

class HomeController extends Controller
{
      /**
      * Single page application catch-all route.
      * @return \Illuminate\Http\Response
      */
      public function index()
      {
         return view('horizon::app'); // what's the meaning of this 'horizon::app'
      }
}
Run Code Online (Sandbox Code Playgroud)

我在 Laravel-Horizo​​n 控制器中发现了这个语法,谁能解释一下:

视图('地平线::应用程序');

是什么意思'horizon::app'

Nor*_*ali 4

就像其他答案所述,这被称为view namespaces. 它不仅限于包的视图,而且您也可以在项目中使用它。

例如,您可能有管理和客户模块,并且希望通过自己的文件夹名称来区分它们的视图,此时您可以使用命名空间声明。例如,您可能有以下文件夹结构:

|- resources
   |- views
      |- admin
         |- index.blade.php 
      |- customer
         |- index.blade.php  
Run Code Online (Sandbox Code Playgroud)

然后,您可以在以下位置注册指向该特定文件夹路径的自己的命名空间AppServiceProvider.php

app('view')->addNamespace('admin', base_path() . '/resources/views/admin');

// or

app('view')->addNamespace('customer', base_path() . '/resources/views/customer');
Run Code Online (Sandbox Code Playgroud)

稍后,在控制器的方法内部,您可以使用以下方式引用它:

return view("admin::index"); 

// or

return view("customer::index");
Run Code Online (Sandbox Code Playgroud)