【问题标题】:Laravel 5.1 blade is not working even after including laravel Collective即使包含 laravel Collective,Laravel 5.1 刀片也无法正常工作
【发布时间】:2016-02-01 06:35:36
【问题描述】:

composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*",
        "laravelcollective/html": "5.1.*"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "pre-update-cmd": [
            "php artisan clear-compiled"
        ],
        "post-update-cmd": [
            "php artisan optimize"
        ],
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

使用工匠命令composer update更新作曲家。然后添加

app.php

  'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Routing\ControllerServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,


        /*
        * Laravel Collective HTML
        */
        Collective\Html\HtmlServiceProvider::class,
        //App\Providers\AnnotationsServiceProvider::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [

        'App'       => Illuminate\Support\Facades\App::class,
        'Artisan'   => Illuminate\Support\Facades\Artisan::class,
        'Auth'      => Illuminate\Support\Facades\Auth::class,
        'Blade'     => Illuminate\Support\Facades\Blade::class,
        'Bus'       => Illuminate\Support\Facades\Bus::class,
        'Cache'     => Illuminate\Support\Facades\Cache::class,
        'Config'    => Illuminate\Support\Facades\Config::class,
        'Cookie'    => Illuminate\Support\Facades\Cookie::class,
        'Crypt'     => Illuminate\Support\Facades\Crypt::class,
        'DB'        => Illuminate\Support\Facades\DB::class,
        'Eloquent'  => Illuminate\Database\Eloquent\Model::class,
        'Event'     => Illuminate\Support\Facades\Event::class,
        'File'      => Illuminate\Support\Facades\File::class,
        'Gate'      => Illuminate\Support\Facades\Gate::class,
        'Hash'      => Illuminate\Support\Facades\Hash::class,
        'Input'     => Illuminate\Support\Facades\Input::class,
        'Inspiring' => Illuminate\Foundation\Inspiring::class,
        'Lang'      => Illuminate\Support\Facades\Lang::class,
        'Log'       => Illuminate\Support\Facades\Log::class,
        'Mail'      => Illuminate\Support\Facades\Mail::class,
        'Password'  => Illuminate\Support\Facades\Password::class,
        'Queue'     => Illuminate\Support\Facades\Queue::class,
        'Redirect'  => Illuminate\Support\Facades\Redirect::class,
        'Redis'     => Illuminate\Support\Facades\Redis::class,
        'Request'   => Illuminate\Support\Facades\Request::class,
        'Response'  => Illuminate\Support\Facades\Response::class,
        'Route'     => Illuminate\Support\Facades\Route::class,
        'Schema'    => Illuminate\Support\Facades\Schema::class,
        'Session'   => Illuminate\Support\Facades\Session::class,
        'Storage'   => Illuminate\Support\Facades\Storage::class,
        'URL'       => Illuminate\Support\Facades\URL::class,
        'Validator' => Illuminate\Support\Facades\Validator::class,
        'View'      => Illuminate\Support\Facades\View::class,

        //Two aliases for HTML Service Provider

        'Form' => Collective\Html\FormFacade::class,
        'Html' => Collective\Html\HtmlFacade::class,

    ],

然后创建了我的视图页面,例如:-

layouts/app.blade.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Laravel Quickstart - Basic</title>
    </head>

    <body>
        <div class="container">
            <nav class="navbar navbar-default">
                <!-- Navebar contents -->
            </nav>
        </div>
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

tasks.blade.php

[![@extends('layouts.app')

@section('content')
    <!-- Current Tasks -->
    @if(count($tasks) > 0)
        <div class="panel panel-default">
            <div class="panel-heading">
                Current Tasks
            </div>

            <div class="panel-body">
                <table class="table table-striped task-table">

                </table>
            </div>
        </div>
     @endif

    <!-- TODO: Current Tasks -->
    @endsection][1]][1]

routes.php [![Route::get('/', function () { //返回视图('欢迎'); $tasks = array('a', 'b', 'c'); 返回视图('tasks', ['tasks'=>$tasks]); });][2]][2]

【问题讨论】:

  • [! [@extend 之前有什么作用?
  • 你试过把它命名为“任务”吗?
  • @luigonsec 没关系 PHP 不区分大小写。
  • @PrafullaKumarSahu 它对命名空间和类名绝对不区分大小写。
  • 如果您发布了一个问题,然后有一个启示并且需要从根本上改变您的问题,只需保持原来的问题不变并提出一个新问题。如果您愿意(并且如果相关),您可以在新问题中引用原始问题,但不要尝试删除您的原始问题。仅当问题不属于 Stack Overflow 时才适合删除(例如,如果您询问属于程序员的猫的猫美容技术......您可以删除它)

标签: php laravel blade


【解决方案1】:

并且不要忘记将这个东西放在你的代码中的 routes.php

Route::group(['middleware' => 'web'], function () {

    //your current code here

})

【讨论】:

    【解决方案2】:

    确保:

    • 将其命名为 Task 而不是 task
    • 在您的 routes.php 中包含 use App\Task
    • Task.php 的第一行包含命名空间:namespace App;(当然,如果您在 App 中有模型)

    【讨论】:

      【解决方案3】:

      您的routes.php 文件顶部可能缺少use App\Task;

      我遇到了完全相同的问题,Class 'Task' not found,这解决了它。 确保您的task::orderBy.... 也是正确的大小写。

      例如,这是我的第一条路线:

      use App\Task;
      use Illuminate\Http\Request;
      
      
      /**
       * Display All Tasks
       */
      Route::get('/', function () {
          $tasks = Task::orderBy('created_at', 'asc')->get();
      
          return view('tasks', [
              'tasks' => $tasks
          ]);
      });
      

      【讨论】:

        【解决方案4】:

        问题是你试图直接访问视图,这不是 Laravel 的工作方式。您需要设置路由并访问该路由,该路由会返回您想要的视图。

        例如,转到app/Http/routes.php。这是您的路线文件。在其中,您可以添加如下内容:

        // The "test" is the uri. Keep this in mind.
        // Think of it as the appended URL.
        // So, it would be something like www.example.com/test
        Route::get('test', function () {
        
            // I am returning the view here.
            // Note that I am returning "tasks", not "tasks.blade.php".
            return view('tasks');
        
        });
        

        然后,根据您的图像,您需要在此处访问此路线:http://localhost/laravel/public/test

        如果您将 URI 更改为“任务”之类的内容:

        // Changing "tests" to "tasks"
        Route::get('tasks', function () {
        
            // Note that I am returning "tasks", not "tasks.blade.php"
            return view('tasks');
        
        });
        

        然后您通过http://localhost/laravel/public/tasks访问它

        阅读有关路由的文档以获取有关该主题的完整细分:http://laravel.com/docs/5.1/routing

        【讨论】:

        • 我的路由正确,但是模型中存在一些问题,所以现在我可以说,我需要修改问题。现在我已经在路由中分配了一个数组并且它正在工作。
        • 你能告诉我为什么 laravel 5.1 中没有模型类吗?
        • 那是一个完全不同的问题和问题。您不应该在同一个问题中混合不同的问题,因为它只会给未来的读者造成混淆。现在,因为您编辑并更改了您的问题,当人们找到这个并阅读答案时,他们将阅读与您的新/编辑问题完全无关的答案。如果你解决了你的问题,你应该标记正确的答案。如果您有新问题,您应该发布一个新问题。
        • 我怎样才能删除这个?我无法删除这个:(
        • 将其标记为删除
        【解决方案5】:

        您的 tasks.blade.php 文件中缺少 @endif

        @extends('layouts.app')
        
        @section('content')
            <!-- Current Tasks -->
            @if(count($tasks) > 0){{--@endif for this section--}}
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Current Tasks
                    </div>
        
                    <div class="panel-body">
                        <table class="table table-striped task-table">
        
                        </table>
                    </div>
                </div>
            @endif{{--Add @endif--}}
            <!-- TODO: Current Tasks -->
        @endsection
        

        【讨论】:

        • @PrafullaKumarSahu 可以运行这个命令 php artisan view:clear 可能是视图缓存问题
        • 仍然没有收获 :( 使用此命令清除缓存后我应该做什么?
        • 另外,您正在加载哪个 URL?您是否像屏幕截图一样直接导航到您的刀片文件?
        • 这不是 @stop 而不是 endsection?
        • @melvnberd here laravel.com/docs/5.1/quickstart 检查它,它是 5.1 中的 endsection
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-03
        • 2015-08-27
        • 2021-11-29
        • 1970-01-01
        • 2016-01-13
        • 2016-03-03
        • 1970-01-01
        相关资源
        最近更新 更多