我按照本教程使用WordPress和Laravel,我能够从Laravel控制器访问WordPress功能.
基本例子
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Corcel;
class WordPressController extends Controller
{
public function getIndex ()
{
return redirect('/');
$posts = get_posts([
'posts_per_page' => 20,
'order' => 'ASC',
'orderby' => 'post_title',
]);
return $posts;
}
Run Code Online (Sandbox Code Playgroud)
这工作,我已经能够访问到目前为止我尝试过的所有WordPress方法.
问题
我遇到的问题是当我创建并注册一个新的artisan命令并尝试从那里访问那些相同的方法时.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Http\Request;
use App\Http\Requests;
class WPTags extends Command
{
protected $signature = 'wp:tags';
protected $description = 'Output tags from WordPress';
public function __construct()
{
parent::__construct();
}
public function handle() …
Run Code Online (Sandbox Code Playgroud) 当我Object.prototype
在 Chrome 控制台中输入内容时,它会显示Object{}
var proto = Object.prototype
如果我将它分配给它显示的变量Object{}
但如果我检查它的原型
var proto = Object.prototype
proto.prototype;
Run Code Online (Sandbox Code Playgroud)
如果proto
是一个Object{}
并且proto.prototype
未定义,这是否意味着它Object()
不Object()
继承任何东西?
另外,这与 相同吗Object.prototype.prototype
?
要返回set
一个序列为1到500之间的随机数的数组,我尝试重构一个标准for循环for(var i = 0; i< 50; i++)
,但是当我尝试使用for in
循环重构时,它没有.我的猜测是,有一些关于array.length
财产的东西,它的使用,我搞砸了.
TL; DR为什么这会返回50个未定义元素的数组,而不是50个随机整数的数组?有没有办法让这种方法有效?
var set = [];
set.length = 50;
for(var i in set){
set[i] = Math.floor((Math.random() * 500) + 1);
}
console.log(set);
Run Code Online (Sandbox Code Playgroud)
类似的问题:有帮助,但不是我想要的
我怀疑我遗漏的一点是设置set.length
没有向数组添加元素,它只创建一个稀疏数组(一个带有间隙的数组).在我的情况下,你不能使用for in
,因为没有任何东西在数组遍历.我要么必须使用虚拟内容(即空字符串)填充数组,或者更逻辑地将我尝试使用.length
属性实现的范围部分分离为单独的range
变量.
工作版
var set = [], range = 50;
for(var i = 0; i < range; i++){
set[i]=Math.floor((Math.random() * 500) + 1);
} …
Run Code Online (Sandbox Code Playgroud) 为什么当我创建arr时,一个数组然后尝试使用for-in循环用整数填充它,当我不初始化它时给我一个错误.我可以告诉它,当我写"var arr"时它被初始化了,但显然不是这样,如果不初始化它,写var var实际上是做什么的.
错误示例
var arr : [Int] //Error Message: Variable 'arr' passed by reference before being initialized
for i in 1...10 {
arr += [i]
}
arr //Error Message: Variable 'arr' used before being initialized
Run Code Online (Sandbox Code Playgroud)
工作实例
var arr : [Int] = [] //Allocating memory?
for i in 1...10 {
arr += [i]
}
arr
Run Code Online (Sandbox Code Playgroud) 我有一个脚本显示数组中的问题,并使用.click
两个按钮上的事件从一个问题切换到下一个问题:上一个和下一个.当我加载页面时,$(":radio").change
选择器工作正常,但是当我点击上一个或下一个时它停止工作.
我尝试将其更改$("#previous)
为console.log以查看是否.click
是罪魁祸首,它似乎正在运行.我认为我的显示功能有问题,但我似乎无法弄清楚原因.
问题数组
var quizQuestions =
[{
question: "1. Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:0
},
{
question: "2. Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:2
},
{
question: "3. Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Laravel 5.1制作一个工匠命令,我甚至无法使用最基本的版本.
采取的步骤
1) php artisan make:console Zelda --command=zelda
2)app/Console/Command/Zelda.php
使用以下内容创建的文件
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class Zelda extends Command
{
protected $signature = 'zelda';
protected $description = 'Command description.';
public function __construct() {
parent::__construct();
}
public function handle() {}
}
Run Code Online (Sandbox Code Playgroud)
3)composer.json中的自动加载器看起来像这样
"autoload": {
"classmap": [
"database",
"app/Console/Commands"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
Run Code Online (Sandbox Code Playgroud)
4)然后我跑php artisan list
,没有塞尔达
我在这里错过了什么?
我知道如何在bash中配置别名,但有没有办法在一个语句中使用两个可能的快捷方式映射别名?
这是两行版本
alias "gac"="git add . && git commit -am "
alias "gitac"="git add . && git commit -am "
Run Code Online (Sandbox Code Playgroud)
我想如果可能的话,解决方案看起来像这样:
伪代码: alias "alias1 || alias2"="command"
这甚至可能与别名或我必须使用一个功能,如果是这样如何?
编写一个转换函数,将数字传递给二进制字符串.该函数正在创建一个合适的二进制序列,但我的比较函数在比较等于binaryIndex[0]
(例如n = 32,16,8,4)的数字时跳过第一个索引.有什么想法吗?
这一步创建了一个二进制有序数组,我将使用它来检查传入的参数:
var Bin = function(n) {
var x =1;
var binSeq=[];
var converted=[];
for (var i=0; x <= n; i++) {
binSeq.unshift(x)
x = x+x
}
console.log(binSeq)
Run Code Online (Sandbox Code Playgroud)
下一步应该比较并吐出一个1和0的二进制序列:但它正在跳过 if (n === binSeq[0])
for (var i=0; i < binSeq.length; i++) {
if ((n - binSeq[i]) >= 0) {
converted.unshift(1);
n=n-binSeq[i]
} else {converted.unshift(0)}
}
console.log(converted)
}
Run Code Online (Sandbox Code Playgroud)
我写了一个工作的Artisan命令,它有两个参数:name
和import
.然后它创建一个带有名称的MySQL数据库,name
并使用import
sql dump 导入它.
MySQLCreate.php
exec("mysql -u homestead -psecret -e \"create database "
. $this->argument('name') . "\"");
exec("mysql -u homestead -psecret " . $this->argument('name')
. "<" . $this->argument('import'));
Run Code Online (Sandbox Code Playgroud)
这有效但我相信有更好的方法来实现这一目标.我担心将密码硬编码到exec
命令中并且非常确定exec
在Laravel应用程序内部使用并不是最好的方法.