簡體   English   中英

Laravel 6.x 背包 NewsCrud 插件

[英]Laravel 6.x Backpack NewsCrud Plugin

我想要做的是允許管理面板使用 newscrud 發布新聞。 我對如何從數據庫中獲取信息並向用戶展示文章感到困惑。

我在使用 laravel 的 newscrud 插件時遇到問題。 我完全不明白如何使用這個插件。 我已經使用作曲家安裝了它。 所有控制器、模型等都在我的 Laravel 項目的供應商文件夾中。

嘗試:我試圖做的是在 laravel-project/vendor/backpack/newscrud/src/app/Http/Controllers/Admin/ArticleCrudController.php 文件中創建一個 function :

public function index()
{
    # Pass the article database information in articles var
    $articles = Articles::all();

    # Return this variable to blog page
    return view ('blog')->with('articles', $articles);
}

在 routes/web.php 文件中:

Route::get('/blog', 'ArticleCrudController@index')->name('blog');

前端代碼

                      @foreach($articles as $article)

                          <div class="col-md-12 d-flex ftco-animate">

                            <div class="blog-entry align-self-stretch d-md-flex">

                              <a href="blog-single.html" class="block-20" style="background-image: url('images/image_6.jpg');">

                              </a>

                              <div class="text d-block pl-md-4">

                                <div class="meta mb-3">

                                  <div><a href="#">July 20, 2019</a></div>

                                  <div><a href="#">Admin</a></div>

                                  <div><a href="#" class="meta-chat"><span class="icon-chat"></span> 3</a></div>

                                </div>

                                <h3 class="heading"><a href="#">Even the all-powerful Pointing has no control about the blind texts</a></h3>

                                <p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>

                                <p><a href="blog-single.html" class="btn btn-primary py-2 px-3">Read more</a></p>

                              </div>

                            </div>

                          </div>

                    @endforeach    

錯誤我得到的錯誤信息是:

Target class [App\Http\Controllers\ArticleCrudController] does not exist. 

如果您需要信息/屏幕截圖,我願意提供。

在 PHP 中使用 composer 時,您不應該修改 /vendor/ 文件夾中的文件。 因為一旦你運行 composer update,它們就會被覆蓋。

如果我理解正確,那么您嘗試做的與背包無關。 您不應覆蓋供應商文件夾中的 package。 您應該在您的應用程序文件夾中創建一個 controller,並在您的路由文件夾中創建一個路由,您通常會在 Laravel 中執行此操作。 只需確保,當您引用 model 時,您引用的是 Backpack\NewsCRUD\app\Models\Article 而不是 App\Models\Article。

希望能幫助到你!

錯誤信息

App\Http\Controllers\ArticleCrudController

顯示 Laravel 無法解析 PHP 命名空間App\Http\Controllers中的 controller 。

我剛剛查看了圖書館的代碼,我建議你更換

Route::get('/blog', 'ArticleCrudController@index')->name('blog');

經過

Route::get('/blog', 'Backpack\\NewsCRUD\\app\\Http\\Controllers\\Admin\\ArticleCrudController@index')->name('blog');

嘗試像這樣編輯您的 web.php:

Route::group([
    'prefix' => '/',
    'middleware' => ['web'],
    // This is all the namespace in directory
    'namespace' => 'laravel-project\vendor\backpack\newscrud\src\app\Http\Controllers\Admin',
], function () {
    Route::get('/blog', 'ArticleCrudController@index')->name('blog');
});

並將您的 ArticleCrudController.php 文件編輯為:

namespace laravel-project\vendor\backpack\newscrud\src\app\Http\Controllers\Admin;

希望這可以幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM