如何将fullcalendar添加到Laravel应用程序中。

huangapple go评论54阅读模式
英文:

How to add fullcallendar to a laravel app

问题

I am trying to integrate fullcalendar in a laravel10 + Metronic app.

helpers.addVendors(['fullcalendar']); called from controller gives error:

Undefined constant "App\Http\Controllers\helpers"

CODE:

<?php
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class CalendarController extends Controller
{
    public function index()
    {
        helpers.addVendors(['fullcalendar']);

        return view('pages/calendar')->with(['page'=>'calendar','section'=>'this_month']);
    }
}

How to add assets from controller? Documentation isn't that clear.

Thank you.

英文:

I am trying to integrate fullcallendar in a laravel10 + Metronic app

helpers.addVendors([&#39;fullcalendar&#39;]); called from controller gives error:

Undefined constant &quot;App\Http\Controllers\helpers&quot;

CODE:

&lt;?php
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class CalendarController extends Controller
{
    public function index()
    {
        helpers.addVendors([&#39;fullcalendar&#39;]);

        return view(&#39;pages/calendar&#39;)-&gt;with([&#39;page&#39;=&gt;&#39;calendar&#39;,&#39;section&#39;=&gt;&#39;this_month&#39;]);
    }
}

How to add assets from controller? Documentation isn't that clear.

Thank you.

答案1

得分: 1

我自己找到了。你需要做的是进入 laravel_app/config/global/pages.php,并将以下内容添加到主数组中:

'calendar' => array(
    'title'  => 'Calendar modified',
    'assets' => array(
        'custom' => array(
            'css' => array(
                'plugins/custom/flatpickr/flatpickr.bundle.css',
                'plugins/custom/fullcalendar/fullcalendar.bundle.css',
                'plugins/custom/datatables/datatables.bundle.css',
            ),

            'js' => array(
                'plugins/custom/flatpickr/flatpickr.bundle.js',
                'plugins/custom/fullcalendar/fullcalendar.bundle.js',
                'plugins/custom/datatables/datatables.bundle.js',

                'js/custom/apps/calendar/mycalendar.js',
            ),
        ),
    ),
),

创建一个路由:

// calendar
Route::get('/calendar', [CalendarController::class, 'index']);

创建一个控制器:

<?php
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class CalendarController extends Controller
{
    public function index()
    {
        return view('pages/calendar')->with(['page'=>'calendar','section'=>'this_month']);
    }
}

laravel_app/resources/views/pages/ 中创建 calendar.blade.php,并从 demo1 calendar.html 添加日历和模态框。

就是这样,希望对其他人有帮助,因为 Metronic 在 Laravel 上的文档并不是很清晰。

英文:

I found it by myself. What you got to do is go to laravel_app/config/global/pages.php and add this to the main array :

&#39;calendar&#39; =&gt; array(
&#39;title&#39;  =&gt; &#39;Calendar modified&#39;,
&#39;assets&#39; =&gt; array(
    &#39;custom&#39; =&gt; array(
        &#39;css&#39; =&gt; array(
            &#39;plugins/custom/flatpickr/flatpickr.bundle.css&#39;,
            &#39;plugins/custom/fullcalendar/fullcalendar.bundle.css&#39;,
            &#39;plugins/custom/datatables/datatables.bundle.css&#39;,
        ),

        &#39;js&#39; =&gt; array(
            &#39;plugins/custom/flatpickr/flatpickr.bundle.js&#39;,
            &#39;plugins/custom/fullcalendar/fullcalendar.bundle.js&#39;,
            &#39;plugins/custom/datatables/datatables.bundle.js&#39;,

            &#39;js/custom/apps/calendar/mycalendar.js&#39;,
        ),
    ),
),

Create a route

// calendar
Route::get(&#39;/calendar&#39;, [CalendarController::class, &#39;index&#39;]);

Create a controller

&lt;?php
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class CalendarController extends Controller
{
    public function index()
    {
        return view(&#39;pages/calendar&#39;)-&gt;with([&#39;page&#39;=&gt;&#39;calendar&#39;,&#39;section&#39;=&gt;&#39;this_month&#39;]);
    }
}

Create calendar.blade.php in laravel_app/resources/views/pages/ and add calendar and modals from the demo1 calendar.html

That is all, hope it helps others, as the Metronic documentation on laravel is not that clear.

答案2

得分: 0

你不应该从你的控制器中添加资源。你使用的代码不是PHP/Laravel代码,所以它不会工作。

你应该总是在你的Blade文件中添加资源,像这样使用脚本标签:

<script src='https://cdn.jsdelivr.net/npm/fullcalendar@6.1.7/index.global.min.js'></script>

然后你可以像官方文档建议的那样使用fullcalendar:https://fullcalendar.io/docs/initialize-globals

英文:

You should never add assets from your controller. Code you used is not PHP/laravel code, so it won't work.

You should always add assets in your blade file, in this case with script tag like this:

&lt;script src=&#39;https://cdn.jsdelivr.net/npm/fullcalendar@6.1.7/index.global.min.js&#39;&gt;&lt;/script&gt;

Then you can use fullcalendar as it's official documentation suggests https://fullcalendar.io/docs/initialize-globals

huangapple
  • 本文由 发表于 2023年5月22日 21:32:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定