douwang4374 2017-06-26 13:41
浏览 68
已采纳

CakePHP 3.4加载jQuery到布局

I started using libraries like jQuery or Twitter Bootstrap with composer. Now I want to load the jquery.min.js from /vendor/components/jquery/ into my /src/Template/Layout/default.ctp.

How can I do that? Because using a normal uri doesn't work:

<script type="text/javascript" src="/vendor/components/jquery/jquery.min.js"></script>

Cake doesn't interpret the file as a file. It looks for a Controller:

Error: VendorController could not be found.

How can I load the JS-file properly?

  • 写回答

2条回答 默认 最新

  • dpm91915 2017-06-26 14:09
    关注

    This question is perhaps a little broad, but you probably want to look into using something like Gulp or Webpack to build your assets from the Composer files to your app's webroot. These will also enable you to concatenate and minimise all your JS/CSS assets to improve performance. Although personally I'd probably use a CDN to deliver a library like jQuery as it is so commonly used on the web that you can benefit from cross-site caching.

    The reason why you are getting an error like "Error: VendorController could not be found." is because Cake can't find the 'vendor' directory inside 'webroot' so is looking for a Controller instead due to the way it handles magic routes (this bit in the routes.php file $routes->fallbacks('DashedRoute');).

    If you don't want to use a tool like Gulp or Webpack you could symlink the relevant files in your webroot folder, but personally I'd use my initial suggestions as these will give you many other benefits. If you do symlink just make sure you don't symlink the entire vendor directory as you don't want to expose all the code in there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?