简体   繁体   English

无法从Laravel 5中的PUBLIC文件夹加载AngularJs文件

[英]Can't load AngularJs files from PUBLIC folder in Laravel 5

I'm having troubles referencing my angularJS files in Laravel's "public" folder. 我在Laravel的“public”文件夹中引用了我的angularJS文件时遇到了麻烦。 My public folder structure looks like this: : 我的公用文件夹结构如下所示

public

   -css

   -js

      -services

      -controllers

      -app.js

I have my "index.php" file inside the "app/views" folder . 我在“app / views”文件夹中有“index.php”文件 When i go to the home page of my application http://localhost , the browser brings me to the index.php file as expected. 当我转到我的应用程序http:// localhost的主页时,浏览器会按预期将我带到index.php文件。

Inside my "index.php" file, i want to reference my javascript files under the "public/js" and I get "GET http://localhost/js/app.js 404 (Not Found)" - in the browser console meaning that those files can't be found. 在我的“index.php”文件中,我想在“public / js”下引用我的javascript文件,并在浏览器控制台中获得“GET http://localhost/js/app.js 404(Not Found)”意味着无法找到这些文件。 These are some of the options i have tried to include them in the "index.php" file: 这些是我试图将它们包含在“index.php”文件中的一些选项:

 1.   <script src="js/app.js"></script>

and

EDIT: 编辑:

 <script src="<?php echo asset('/js/app.js'); ?>">
<script src="<?= url('js/app.js') ?>">

and

  1. I have tried to move the js/app.js to folders resources or resources/views . 我试图将js / app.js移动到文件夹资源或资源/视图。
  2. I have tried to move just the app.js to resources/views 我试图将app.js移动到资源/视图

All these options don't find the files under the "public" folder. 所有这些选项都找不到“public”文件夹下的文件。 I don't use BLADE 我不使用BLADE

My files look like : 我的文件看起来像:

index.php 的index.php

<!DOCTYPE html>
<html lang=eng>
<head>
<meta charset="UTF-8">
  <title>Welcome </title>

  <!-- CSS -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css"> <!-- load bootstrap via cdn -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <!-- load fontawesome -->
  <style>
    body    { padding-top:30px; }
/*    form    { padding-bottom:20px; }
*/  </style>

  <!-- JS -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

    <script src="js/app.js">
    </script> <!-- load our application --><!-- load our application -->
</head>


<body   class="container" ng-app="user1App" ng-controller="mainController">

  <div class="user1"  ng-repeat="user1 in user1s">
    <h3> <small>{{ user1.name }} , {{ user1.email }}</small></h3>
    <p>{{ user1.phone_number }}</p>

  </div>

</body>
</html>

app.js app.js

angular.module('user1App', [
]);

angular.module('user1App').controller('mainController', function($scope, $http, User1, $sce) {

$http.get('api/v1/jokes').success(function(data) { 

$scope.user1s = data; 

});
}

My config/view.php looks like : 我的config / view.php看起来像

'paths' => [
    realpath(base_path('resources/views')),
],

For more information about my code look here AngularJS won't display (parse) data that is delivered with $http.get in Laravel5 有关我的代码的更多信息,请查看此处AngularJS将不会显示(解析)与Laravel5中的$ http.get一起提供的数据

ERROR in console : 控制台中的错误:

GET http://localhost/js/app.js 404 (Not Found)

.htaccess 的.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Please help ,I'm realy not seeing the problem and can't google it anywhere. 请帮助,我真的没有看到问题,不能谷歌它在任何地方。

Thanks 谢谢

Leave your files at public/js and retrieve it like: 将文件保留为public/js并检索它:

<script src="<?= url('js/app.js') ?>">

You mentioned doing <script src="{{ asset('/js/app.js') }}"> , but it should contain js/app.js instead of /js/app.js and obviously replace {{ }} with php tags if you are not using blade. 你提到过<script src="{{ asset('/js/app.js') }}"> ,但它应该包含js/app.js而不是/js/app.js ,显然要替换{{ }}如果您不使用刀片,请使用php标签。

If you don't use blade 如果你不使用刀片

  <script src="{{ asset('/js/app.js') }}">

Its will not work, because, '{{' is blade's feature, so what you can do is: 它不起作用,因为'{{'是刀片的功能,所以你可以做的是:

  <script src="<?php echo asset('/js/app.js'); ?>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM