我已经阅读了许多消息来源,暗示laravel facade最终是为了方便而存在,而且应该注入这些类以允许松散耦合.甚至Taylor Otwell也有一篇文章解释了如何做到这一点.似乎我不是唯一一个想到这一点的人.
use Redirect;
class Example class
{
public function example()
{
return Redirect::route("route.name");
}
}
Run Code Online (Sandbox Code Playgroud)
会成为
use Illuminate\Routing\Redirector as Redirect;
class Example class
{
protected $redirect;
public function __constructor(Redirect $redirect)
{
$this->redirect = $redirect
}
public function example()
{
return $this->redirect->route("route.name");
}
}
Run Code Online (Sandbox Code Playgroud)
这很好,除了我开始发现一些构造函数和方法开始采用四个+参数.
由于Laravel IoC 似乎只注入类构造函数和某些方法(控制器),即使我有相当精益的函数和类,我发现类的构造函数正在填充所需的类,然后将其注入到需要的方法.
现在我发现如果我继续这种方法,我将需要自己的IoC容器,如果我使用像laravel这样的框架,感觉就像重新发明轮子一样?
例如,我使用服务来控制业务/视图逻辑而不是处理它们的控制器 - 它们只是路由视图.所以控制器首先取其对应的service
,然后是parameter
其url.一个服务功能还需要检查表单中的值,因此我需要Request
和Validator
.就像那样,我有四个参数.
// MyServiceInterface is binded using the laravel container
use Interfaces\MyServiceInterface;
use Illuminate\Http\Request; …
Run Code Online (Sandbox Code Playgroud) 我有一些方法可以返回两种返回类型之一 - (我正在使用一个利用MCV的框架,所以特别重构这几个函数并不吸引人)
是否可以声明返回类型返回一个或另一个并失败的其他任何东西?
function test(): ?
{
if ($this->condition === false) {
return FailObject;
}
return SucceedObject;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用这种方法在javascript中制作人工"哈希映射".我所瞄准的只是键值对,实际运行时间并不重要.下面的方法工作正常.
还有其他方法可以循环使用吗?
for (var i in a_hashMap[i]) {
console.log('Key is: ' + i + '. Value is: ' + a_hashMap[i]);
}
Run Code Online (Sandbox Code Playgroud)
我遇到一个问题,当第一个键只包含一个条目时,它会在第一个键后输出一堆未定义的键.我有一种感觉,因为代码是在一个使用i的循环中,即使我在调试时也不应该发生.我也无法改变i,因为for循环似乎根本不理解替换的var.
任何想法?
我希望我的测试文件夹与我的应用程序代码分开。我的项目结构是这样的
myproject/
myproject/
myproject.py
moduleone.py
tests/
myproject_test.py
Run Code Online (Sandbox Code Playgroud)
我的项目.py
from moduleone import ModuleOne
class MyProject(object)
....
Run Code Online (Sandbox Code Playgroud)
myproject_test.py
from myproject.myproject import MyProject
import pytest
...
Run Code Online (Sandbox Code Playgroud)
我使用myproject.myproject
因为我使用命令
python -m pytest
Run Code Online (Sandbox Code Playgroud)
从项目根目录 ./myproject/
但是,然后这些模块中的导入失败
E ModuleNotFoundError: 没有名为“moduleone”的模块
我正在运行 Python 3.7 并已阅读自 3.3 以来,__init__
不再需要空文件,这意味着我的项目成为隐式命名空间包
但是,我尝试在其中添加__init__.py
文件myproject/myproject/
并尝试在其中添加conftest.py
文件myproject/
但都不起作用
我已经阅读了一些答案,这些答案说要弄乱路径,然后在其他问题中投票说不要。
什么是正确的方法,我错过了什么?
编辑;
可能相关,我使用 arequirements.txt
使用 pip 安装 pytest。这可能有关系吗?如果是这样,在这种情况下安装 pytest 的正确方法是什么?
编辑2:
其中的路径之一sys.path …
在面向对象的样式中,依赖关系倾向于被反转,构造函数具有不同的Spartan角色.它唯一的工作是确保对象初始化为满足其基本不变量的状态(换句话说,它确保对象实例以有效状态启动,而不是更多).
这是一个类的基本示例.在创建类时,我传入需要解析的HTML,然后设置类属性.
OrderHtmlParser
{
protected $html;
protected $orderNumber;
public function __construct($html)
{
$this->html = $html;
}
public function parse()
{
$complexLogicResult = $this->doComplexLogic($this->html);
$this->orderNumber = $complexLogicResult;
}
public function getOrderNumber()
{
return $this->orderNumber;
}
protected function doComplexLogic($html)
{
// ...
return $complexLogicResult;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用它
$orderparser = new OrderHtmlParser($html);
$orderparser->parse()
$orderparser->getOrderNumber();
Run Code Online (Sandbox Code Playgroud)
我使用一个parse
函数,因为我不希望构造函数做任何逻辑,因为上面的文章和本文都说这是一个糟糕的做法.
public function __construct($html)
{
$this->html = $html;
$this->parse(); // bad
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我不使用该parse
方法,那么我的所有属性(在此示例中只是一个)将返回null
.
这被称为"无效状态"中的对象吗?
另外,有点感觉 …
我在/my/example/page
链接到laravel路线的页面上有一个链接my.route
.
路线
Route::group(['middleware' => ['auth']], function () {
Route::group(['middleware' => ['Some\Custom\Auth\Middleware:1']], function () {
Route::match(['GET', 'POST'], '/my/route/to/external/controller', 'exampleController@externalLink')->name('my.route');
}
}
Run Code Online (Sandbox Code Playgroud)
链接 /my/example/page
<a href="/my/route/to/external/controller">Link</a>
Run Code Online (Sandbox Code Playgroud)
此路由/my/route/to/external/controller
指向控制器externalLink
中的此控制器方法,该方法exampleController
返回要使用的href的url
public function externalLink()
{
return $this->redirect->away('www.externalsite.com');
}
Run Code Online (Sandbox Code Playgroud)
我的考试是
$this->visit(/my/example/page)
->click('Link')
->assertRedirectedToRoute('my.route');
Run Code Online (Sandbox Code Playgroud)
我不断得到错误
Symfony的\分量\ HttpKernel \异常\ NotFoundHttpException
当我使用click()
测试方法.
我可以使用它,@expectedException
但他没有帮助,因为我希望看到一个不同的页面.
我也试过(不在一起);
->assertResponseStatus(200);
->seePageIs('www.externalsite.com');
->assertRedirect();
->followRedirects();
Run Code Online (Sandbox Code Playgroud)
从浏览器检查,当点击网址时,我得到了
http://www.example.com/my/route/to/external 302
http://www.externalsite.com 200
Run Code Online (Sandbox Code Playgroud)
如何在功能上测试被点击的按钮并重定向到外部站点?
我们有一些传统的laravel项目,它们在课堂上使用外墙.
use Cache;
LegacyClass
{
public function cacheFunctionOne()
{
$result = Cache::someFunction('parameter');
// logic to manipulate result
return $result;
}
public function cacheFunctionTwo()
{
$result = Cache::someFunction('parameter');
// different logic to manipulate result
return $result;
}
}
Run Code Online (Sandbox Code Playgroud)
我们最近的项目使用依赖注入外墙所代表的层级类,正如Taylor Otwell本人所暗示的那样.(我们对每个类使用构造函数注入,但为了保持示例简短,这里我使用方法注入并使用单个类.)
use Illuminate\Cache\Repository as Cache;
ModernClass
{
public function cacheFunctionOne(Cache $cache)
{
$result = $cache->someFunction('parameter');
// logic to manipulate result
return $result;
}
public function cacheFunctionTwo(Cache $cache)
{
$result = $cache->someFunction('parameter');
// different logic to manipulate result
return …
Run Code Online (Sandbox Code Playgroud) 我很尴尬地问这个并且它很可能是重复的,但是我的谷歌搜索结果很短(我想错误地搜索)并且这样一个基本问题激怒了我.
我有一个包含我不知道的值的数组.
在java中,看看第二个条目,我会使用类似的东西
var = array[1]
Run Code Online (Sandbox Code Playgroud)
我理解Php数组是键值对,但是我怎样才能简单地查看数组中的第n个值来查看它的键值对,甚至更好,然后只访问键/值?
我正在尝试流浪汉,我看到当我运行流浪汉时,流浪者的盒子里已经有了一个authorized_keys
文件~/.ssh/
里面是一个rsa键.这个密钥有什么区别,如果我id_rsa.pub
自己使用创建公钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Run Code Online (Sandbox Code Playgroud) 我想绑定我的前进和后退鼠标按钮(4,5)以在之前的光标位置之间跳转.我有我的档案
~/.config/sublime-text-3/Packages/User/Default (Linux).sublime-mousemap
Run Code Online (Sandbox Code Playgroud)
我的代码是这样的
[
{
"button": "button4",
"command": "jump_back"
},
{
"button": "button5",
"command": "jump_forward"
}
]
Run Code Online (Sandbox Code Playgroud)
我也试过了
{
"button": "button5",
"modifiers" : [],
"command": "jump_forward"
}
Run Code Online (Sandbox Code Playgroud)
乃至
{
"button": "button5",
"modifiers" : ["alt"],
"command": "jump_forward"
}
Run Code Online (Sandbox Code Playgroud)
但始终使用默认的next/prev选项卡设置.但是,使用sublime默认密钥绑定可以正常工作
"alt" + "-"
"shift" + "alt" + "-"
Run Code Online (Sandbox Code Playgroud) php ×5
laravel ×3
testing ×2
arrays ×1
docker ×1
hashmap ×1
javascript ×1
oop ×1
php-7 ×1
phpunit ×1
pytest ×1
python ×1
python-3.x ×1
rsa ×1
sublimetext ×1
type-hinting ×1