出于某种原因,我的 jQuery 拒绝加载。我收到一个错误:
参考错误:$ 未定义
我的模板在views/layouts/editor.blade.php,我正在像这样在模板的头部加载 jQuery
<script src="{{ asset('js/jquery.min.js') }}"></script>
Run Code Online (Sandbox Code Playgroud)
我可以看到 jQuery 已加载到我的控制台中。但我仍然收到错误。
我正在使用 laravel mix 编译我的 javascript,我的 webpack.mix.js 文件如下所示:
// javascript
mix.js('resources/assets/js/app.js', 'public/js')
.js('node_modules/jquery/dist/jquery.min.js', 'public/js')
.autoload({
jquery: ['$', 'window.jQuery', 'jQuery'],
});
// css
mix.sass('resources/assets/sass/app.scss', 'public/css')
.sass('node_modules/bootstrap/scss/bootstrap.scss', 'public/css');
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
编辑:
我的模板文件如下所示:
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Styles -->
<link href="{{ asset('css/bootstrap.css') }}" …Run Code Online (Sandbox Code Playgroud) 我不明白这个?古怪?完全是php ...因为我正在查看其他人的代码,我看到有些人完全忽略了else语句而只是放置了"return false".声明.
似乎这个技巧仅适用于return语句,正如您在下面的情况中所看到的,它在回显文本时不起作用.
这很奇怪,比如两个例子,肯定这个函数是readuraly读的,所以函数会在if语句中返回"true"因为满足条件但是当它离开if/else语句时它应该返回FALSE因为它有没有ELSE声明.这不会发生,函数仍然返回true.
我无法理解这一点,所以希望有人可以解释一下?
// Case 1
function checkNumber1($number) {
if ($number === 10) {
return true;
} else {
return false;
}
}
$number = 10;
var_dump(checkNumber1($number)); // Returns true
// Case 2
function checkNumber2($number) {
if ($number === 10) {
return true;
}
return false;
}
$number = 10;
echo '<br>';
var_dump(checkNumber2($number)); // Also returns true??
// Case 3
function checkNumber3($number) {
if ($number === 10) {
echo 'true' . '<br>';
} else { …Run Code Online (Sandbox Code Playgroud) 试图掌握 Mustache ......这里的问题是条件“if/else”没有按预期工作。如果用户是法国人,我正在检查数据......如果他们显示为真,否则显示为假。
问题
Mustache 完全忽略了条件并为每个表格行显示 true 和 false。
我的代码
我的标记
<!-- -->
<h3>Looping through people</h3>
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Language</th>
</tr>
</thead>
<template id="template2">
{{#users}}
<tr>
<td>{{name}}</td>
<td>{{age}}</td>
<td>{{language}}</td>
{{#is_french}}
<td>True</td>
{{/is_french}}
{{^is_french}}
<td>False</td>
{{/is_french}}
</tr>
{{/users}}
</template>
<tbody id="table2"></tbody>
</table>
<!-- -->
Run Code Online (Sandbox Code Playgroud)
我的jQuery
$(function(){
/**
* conditional data
*/
template = $('#template2').html();
data = {
'users': [{
name: 'John',
age: 22,
language: 'French',
is_french: true,
},
{
name: 'Billy',
age: 33,
language: 'Anglaise',
is_french: false, …Run Code Online (Sandbox Code Playgroud) 我在窗口上设置了一个 vue 实例,main.js如下所示:
window.todoEventBus = new Vue()
Run Code Online (Sandbox Code Playgroud)
在我的组件中,我试图像这样访问这个 todoEventBus 全局对象:
created() {
todoEventBus.$on('pluralise', this.handlePluralise);
},
Run Code Online (Sandbox Code Playgroud)
但我收到错误说:
Failed to compile.
./src/components/TodoItem.vue
Module Error (from ./node_modules/eslint-loader/index.js):
error: 'todoEventBus' is not defined (no-undef) at src\components\TodoItem.vue:57:9:
55 |
56 | created() {
> 57 | todoEventBus.$on('pluralise', this.handlePluralise);
| ^
58 | },
59 |
60 | methods: {
1 error found.
Run Code Online (Sandbox Code Playgroud)
但是,如果我在 console.log todoEventBus 中看到 vue 对象。
我的 package.json 文件看起来像这样。
{
"name": "todo-vue",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve", …Run Code Online (Sandbox Code Playgroud) 我正在向 发送 post 请求http://localhost/projects/webdevlist/public/api/register并收到 405 错误:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: GET, HEAD.
Run Code Online (Sandbox Code Playgroud)
路线\api.php:
<?php
use Illuminate\Http\Request;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/register', 'Api\AuthController@register');
Run Code Online (Sandbox Code Playgroud)
Api\AuthController.php:
<?php
namespace App\Http\Controllers\Api;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AuthController extends Controller
{
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:55',
'email' => 'required|email',
'password' => 'required|confirmed'
]);
$user = User::create($validatedData);
$accessToken = $user->createToken('token')->accessToken;
return response(['user' => $user, 'access_token' …Run Code Online (Sandbox Code Playgroud) 我需要显示三个日历,一个用于当前月份,另外两个用于接下来的两个月。
我正在使用 Carbon 进行这些计算。
今天是10月31日。
如果我写以下
$carbon = Carbon::now('UTC'); // current datetime in UTC is 8:54 AM October 31, 2016
echo $carbon->format('F') . '<br>';
echo $carbon->addMonths(1)->format('F');
Run Code Online (Sandbox Code Playgroud)
我得到这个输出
十月
十二月
我完全错过了 11 月……所以我如何在 10 月加上一个月才能得到 11 月。
我有一个表格
<form action="{{ action('NotesController@store') }}" method="post">
//
</form>
Run Code Online (Sandbox Code Playgroud)
该操作指向目录中storeNotesController 控制器中的方法App\Http\Controllers。
该NotesController如下所示:
<?php
namespace App\Http\Controllers;
use App\Note;
use Illuminate\Http\Request;
class NotesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个辅助函数,它可以帮助我在 MVC 应用程序中“添加”路由(映射到控制器/操作)。
我的index.php 文件如下所示。
// use block
use App\Router;
use App\Config;
use App\Core\Database;
use App\Bootstrap;
// dependencies
$router = new Router;
$config = new Config;
$database = new Database($config);
$bootstrap = new Bootstrap($router, $database);
// routes
require '../App/Routes.php';
$bootstrap->router->dispatch($_SERVER['QUERY_STRING']);
Run Code Online (Sandbox Code Playgroud)
我的“路线”文件中有一条路线,我的路线如下所示。
$bootstrap->router->add('/home', ['controller' => 'home', 'action' => 'index']);
Run Code Online (Sandbox Code Playgroud)
我更愿意像这样简单地将根添加到我的文件中......
route('/home', ['controller' => 'home', 'action' => 'index']);
Run Code Online (Sandbox Code Playgroud)
所以我需要做一个辅助函数
问题
感谢 psr-4 自动加载,我知道如何在需要时提取类,但是当涉及到平面旧函数时,这一点不太清楚。如何将辅助函数干净地添加到 MVC 框架中,而无需到处添加 require 文件?
非常感谢您的时间和考虑!
背景:
在大型控制器内部,我将调用许多不同的功能,创建用户,更新订单,在事件中安排.这些操作中的每一个都由模型层中的函数处理...以下是其中一个函数的示例:
$user_id = 1;
$data = array('name' => 'Billy');
if (updateUser($user_id, $data) === false) {
// handle error?
}
// continue with rest of controller
Run Code Online (Sandbox Code Playgroud)
问题:
我终于在今天进行了现实检查,并意识到我没有充分的理由像这样编码......
如果updateUser()返回false,那么我的数据库抽象层出现严重问题,导致我无法更新数据库中的数据.这绝不应该发生,因此无论如何都不会向我的用户显示实际错误(这将允许他们采取适当的行动).
基本上我的应用程序从根本上打破了.
题:
我应该懒得检查功能应该不会返回假的?如果是这样的话?或者我应该在没有任何检查的情况下直接打电话给他们
updateUser($foo)
createBooking($bar)
scheduleEvent($qux)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码使用ajax上传图像:
function saveToServer(file) {
const fd = new FormData();
fd.append('image', file);
$.ajax({
method: 'post',
data: {
image: fd,
diaryHash: "{{ $diary->hash }}",
entryHash: "{{ $entry->hash }}",
},
processData: false,
contentType: false,
url: "{{ action('EntriesController@storeImage') }}",
success: function(data, textStatus, jqXHR){
const url = JSON.parse(xhr.responseText).data;
insertToEditor(url);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我正在传递要存储的图像,我正在传递另外两个变量,diaryHash并且entryHash.
我没有在控制器中获取传递的变量,因为processData设置为false.
使用AJAX进行图像上传时,如何将数据与图像一起传递?
我有一个控制器从我的数据库中获取用户日记的数组并将它们传递给我的视图:
<?php
public function readDiaries($hash)
{
$user = User::where('hash', $hash)->first();
$diaries = Diary::where('user_id', $user->id)->get();
return view('app.diary.readDiaries', ['diaries' => $diaries]);
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我正在使用循环遍历日记@foreach.
<div id="diaries" class="card-columns">
@if (count($diaries) > 0)
@foreach ($diaries as $dairy)
{{ var_dump($diary) }}
@endforeach
@endif
</div>
Run Code Online (Sandbox Code Playgroud)
但我得到以下未定义的变量错误...
未定义的变量:日记(查看:C:\ xampp\htdocs\personal_projects\Active\diary_app\resources\views\app\diary\readDiaries.blade.php)
为什么我的$ diary变量在@foreach循环中未定义?
问题
我正在尝试使用PayPal Checkout REST SDK,它需要PayPal库通过composer自动加载.我已经完成了在CodeIgniter 3中启用Composer的步骤,但是当我转到我自动加载PayPal\Rest\ApiContext类的控制器时,我收到以下错误:
致命错误:在第15行的C:\ xampp\htdocs\php\toucan-talk\App\modules\paypal\controllers\Paypal.php中找不到类'PayPal\Rest\ApiContext'
到目前为止我有什么
这是我的composer.json文件
{
"require": {
"paypal/rest-api-sdk-php" : "*"
}
}
Run Code Online (Sandbox Code Playgroud)
我$config['composer_autoload'] = TRUE;在config.php文件中设置了.
这是我的控制器
<?php
use PayPal\Rest\ApiContext;
class Paypal extends MX_Controller
{
public function __construct()
{
$api = new ApiContext(
);
var_dump($api);
}
}
Run Code Online (Sandbox Code Playgroud)
题
如何对composer及其自动加载器进行故障排除,以便确定自动加载进程失败的位置.
php ×9
laravel ×5
javascript ×3
jquery ×3
laravel-5 ×2
ajax ×1
autoload ×1
codeigniter ×1
composer-php ×1
controller ×1
datetime ×1
eslint ×1
foreach ×1
frameworks ×1
helper ×1
laravel-5.5 ×1
laravel-mix ×1
mustache ×1
npm ×1
php-carbon ×1
post ×1
psr-4 ×1
rest ×1
templating ×1
time ×1
vue.js ×1