douke7274 2018-03-13 16:46
浏览 367
已采纳

require_once()在Laravel代客托管的项目中打开资源失败

I am just picking up using Laravel, but I dont like Vue and have been working with the React ecosystem and would like to use React instead of Vue. Laravel Mix doesnt give me the setup I want and so I figured I could use create-react-app.
Using Laravel Valet, I have started a project in which I have also installed create-react-app in a folder called ui, at the root of the Laravel installation.

My idea is to forego some of Laravel's functionality, namely the whole frontend.

I am attempting to require the react app build html file in resources/views/main.blade.php like so: require_once __DIR__.'/ui/build/index.html';

This gives me the error:

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Illuminate\View\Engines\PhpEngine::main(): Failed opening required '/Users/Username/Sites/sitename/storage/framework/views/ui/build/index.html' (include_path='.:')

This path is not correct, but I'm not sure why it is inserting /storage/framework/ into that path.

I have also tried the following, each with a similar error of Failed opening resource:

require_once('../../ui/build/index.html'); require_once($_SERVER['DOCUMENT_ROOT'].'/ui/build/index.html'); require_once(dirname(__FILE__).'/ui/build/index.html');

  • 写回答

1条回答 默认 最新

  • douniuta4783 2018-03-13 17:19
    关注

    Laravel does not integrate with Vue, and Laravel Mix is a simple layer on top of Webpack. The default Laravel application ships with Vue scaffolding but removing that is as simple as deleting the files in resources/assets/js and if you wish to use another Javascript library then you can add that into your app.js instead.

    The error you're receiving is because Laravel caches view files, meaning that they're served from the cache directory (which lives in storage/framework) so references like __DIR__ are referencing the cache directory, not the resources directory. You can see this in the documentation:

    You should avoid using the DIR and FILE constants in your Blade views, since they will refer to the location of the cached, compiled view.

    The correct approach to include files into your views with Blade is using the @include directive, e.g:

    @include('ui.build.index')
    

    Also, worth noting, that any time you do need to obtain the path to a file in your Laravel application you should use the base_path and app_path helpers.

    Prior to continuing with development of your application you should read through the JavaScript & CSS Scaffolding documentation and the Blade documentation, as they contain a lot of information that will be very useful to you — for example, it explains how to replace Vue with React using a single command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题