我想启用重写日志来调试一些给我带来问题的重写规则.所以我在httpd.conf文件的末尾添加了这些行:
<IfModule mod_rewrite.c>
RewriteLog "/logs/rewrite.log"
RewriteLogLevel 4
</IfModule>
Run Code Online (Sandbox Code Playgroud)
我做的下一件事是重启Apache.但它会导致错误,并且无法启动.这是我在XAMPP控制面板中得到的:
13:14:56 [Apache] Error: Apache shutdown unexpectedly.
13:14:56 [Apache] This may be due to a blocked port, missing dependencies,
13:14:56 [Apache] improper privileges, a crash, or a shutdown by another method.
13:14:56 [Apache] Check the "/xampp/apache/logs/error.log" file
13:14:56 [Apache] and the Windows Event Viewer for more clues
Run Code Online (Sandbox Code Playgroud)
我在error.log中没有任何线索.实际上,当发生此错误时,不会生成任何行.
我还尝试更改RewriteLog行以使用绝对路径:
RewriteLog "c:\xampp\apache\logs\rewrite.log"
Run Code Online (Sandbox Code Playgroud)
请你帮助我好吗?
嗯,这在技术上是可行的,但这会打破MVC架构吗?
我不确定在控制器和型号之间是否建议使用这种类型的通信.我将使用一个简单的示例和两种方法来描述它:
选项1(模型抛出异常,控制器捕获它):
class Controller {
private $model;
public function save($data) {
try {
$this->model->save($data);
} catch (Exception $e) {
// handle exception
}
}
}
class Model {
public function save($data) {
// Call to internal function to save data in BD
if (! $this->_save($data)) throw new Exception('Error saving data');
}
}
Run Code Online (Sandbox Code Playgroud)
选项2(控制器完全处理异常):
class Controller {
private $model;
public function save($data) {
try {
if (! $this->model->save($data)) throw new Exception('Error saving data');
} catch (Exception $e) {
// handle …
Run Code Online (Sandbox Code Playgroud) 我一直在寻找其他类似的帖子,问题似乎是一个未转义的斜线.但是我逃避了他们.
这是字符串的外观:
23/12/2012
这就是我宣布验证规则的方式:
regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)[0-9]{2}$/]
Run Code Online (Sandbox Code Playgroud)
结束分隔符在那里,日期的两个中间斜杠用反斜杠进行转义.我也试过这个略有不同,但我得到了同样的错误:
regex_match[/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/]
Run Code Online (Sandbox Code Playgroud)
哪里出错?
编辑:
根据您的建议,我尝试使用回调函数.这是声明,它位于正在执行表单验证的控制器类中:
function mach_date($date) {
/* DEBUG */ echo 'Here I am!'; exit; // execution should stop here displaying the echo
return (bool)preg_match('/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/', $date);
}
Run Code Online (Sandbox Code Playgroud)
application/config/form_validation.php中的验证规则:
$config = array(
// other validation groups.....,
'articles' => array(
// other validated fields.....,
array(
'field' => 'date_p',
'label' => 'Publishing date',
'rules' => 'callback_match_date'
)
)
);
Run Code Online (Sandbox Code Playgroud) 当CSRF令牌过期时,我不喜欢CI在默认情况下的行为方式.例如,当用户长时间显示登录表单并最终提交时,会出现带有错误消息的丑陋空白页面.
我想办法绕过这个,有人告诉我扩展Security类,并覆盖它的csrf_show_error()方法,如下所示:
class MY_Security extends CI_Security {
public function __construct()
{
parent::__construct();
}
public function csrf_show_error()
{
// comment out the default action
// show_error('The action you have requested is not allowed.');
// add redirect actions here
// I'd like to use some helper function here like redirect()
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我不能,或者我不知道如何从这里访问库和帮助程序,以便执行重定向.我不能在这里使用get_instance()函数,因为我收到错误.那么,我还能做些什么呢?或者还有其他更好的选择来阻止显示此错误页面吗?
根据下一个例子:
class InvoiceGenerator
{
function create(Invoice $invoice)
{
$invoice->create();
}
}
class InvoiceGenerator
{
function create($invoiceData)
{
$invoice = new Invoice();
$invoice->create($invoiceData);
}
}
Run Code Online (Sandbox Code Playgroud)
第一个示例在InvoiceGenerator和Invoice类之间的耦合较少,因为InvoiceGenerator不需要Invoice类.此外,它不仅可以处理一个类,而且可以处理几乎没有修改的整个界面.我已多次读过这样的原则:接口代码,而不是实现.这种情况的缺点是我被迫在客户端代码中实例化Invoice类.
第二个有更多的封装.实例化和创建发票的所有过程都委托给InvoiceGenerator类.尽管两个类都是耦合的,但这是有道理的,因为"发票生成器"在没有发票的情况下不会做任何事情.
您认为哪种最合适?或者两者之间的平衡设计的关键点是什么?
我可以使用至少两种基本方法从子类访问受保护的类方法:
parent::myMethod();
$this->myMethod();
Run Code Online (Sandbox Code Playgroud)
如果我不需要在子类中重写它,在这种情况下我将不得不这样做:
function myMethod() {
...
parent::myMethod();
...
}
Run Code Online (Sandbox Code Playgroud)
这是最推荐的方式吗?我个人觉得使用parent :: myMethod()而不是$ this-> myMethod更舒服,因为第一个立即告诉我这个方法是继承的.但我不确定哪种方式在性能和最佳实践方面.
编辑:
检查一下,这是我的问题的真实案例.它使用CodeIgniter,但即使你不熟悉它,你也可能会得到它:
class Admin_Controller extends CI_Controller {
protected function validate_form($validation) {
$this->load->library('form_validation');
// This will validate the form sent against the validation rules group specified in $validation
if ($this->form_validation->run($validation) == false) {
throw new Exception('There are errors in the form');
}
}
}
class Articles extends Admin_Controller {
function save($id) {
$this->validate_form(strtolower(get_class($this));
// OR
parent::validate_form(strtolower(get_class($this));
// Other actions …
Run Code Online (Sandbox Code Playgroud) 请有人用这些模式来清理我头脑中的混乱:
我见过一些网站两者都是相同的(指挥链的示例与责任链相同),而其他网站则不同。
这是我对每一个的理解:
命令链:
我们将类称为 CommandChain,其属性包含“命令”列表,这些命令是实现相同接口的类的实例。假设他们都必须实现 onCommand(command,arguments)。
CommandChain 具有用于在其中注册新命令的 addCommand() 方法,以及接受命令名称及其参数的 runCommand() 方法。此方法应循环遍历命令列表,直到其中一个命令响应、执行相应操作并发送 ok。
责任链
正如我在某些站点中看到的那样,这几乎是相同的,但有一个区别:每个命令实例将存储对下一个命令实例的引用,而不是让类存储要循环的命令列表。
那么,这种差异是否大到足以认为两种设计模式不同?
它们适用于哪些实际案例?
php design-patterns web-applications chain-of-responsibility
我不确定如何在UML类图中表示这种情况。
我的意思的例子:
一个典型的Mysql类处理数据库连接,但将语句的构造和执行委托给另一个名为MysqlStatement的类,以便Mysql类具有一个名为Mysql :: prepare()的方法,该方法返回Mysqlstatement类(不包含该类),例如所以:
class Mysql extends DB_Connection {
public function connect($user, $pass, $dbhost, $dbname)
{
$this->dbh = mysql_connect($dbhost, $user, $pass);
// ...
}
/**
* Connects to the DB, then creates and returns the statement object
* @param string $query
* @return MysqlStatement
*/
public function prepare($query)
{
if (! $this->dbh) $this->connect();
return new MysqlStatement($this->dbh, $query);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,据我所知,没有组成/聚合/关联,因为没有一个类包含另一个。因此,我不确定如何表示这种关系。我需要这样做以使一切井井有条,并对系统有所了解。
我正在尝试设置联系表单,我很难让它使用CI中的SMTP配置.但是我已经成功尝试了基本的'mail'配置.
简而言之,我正在使用这个简单的代码进行测试:
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.1and1.es',
'smtp_port' => 25,
'smtp_user' => 'user@mysite.com',
'smtp_pass' => '********',
'mailtype' => 'text',
'charset' => 'utf-8',
'wordwrap' => true,
'wrapchars' => 50
);
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('my_real_email_address@gmail.com');
$this->email->to('my_real_email_address@gmail.com');
$this->email->reply_to('my_real_email_address@gmail.com', 'John Doe');
$this->email->subject('Message sent from the contact form in website');
$this->email->message('Body of message...');
if ($this->email->send()) {
return true;
}
echo '<!--' . print_r($this->email->print_debugger(), true) . '-->';
return false;
Run Code Online (Sandbox Code Playgroud)
我刚刚问过我的托管服务提供商,所有的SMTP配置都是正确的.他们还检查了我的电子邮件帐户是否正常工作.
事实上,如果我选择:
protocol =>'mail'
消息传递没有问题.AFAIK php mail()函数也使用托管服务提供商SMTP,但它只是将其交给服务器并忘记它.
相反,我想使用SMTP身份验证,担心发送电子邮件.但只能通过改变
protocol =>'smtp'
当我发送表单时有很多处理时间,我终于得到了这个调试消息:
220 smtp.1and1.es (mreu2) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试访问IonList组件的closeSlidingItems方法,以便一旦我单击将该项目向右滑动后从后面出现的按钮,滑动项目就会自动关闭。
我尝试通过放置对 IonList 的引用,然后从该按钮上的单击事件的回调方法访问它来实现此目的。但是,我收到错误:
无法读取未定义的属性“closeSlidingItems”
这是根组件中的代码:
<template>
<ion-app>
<ion-list ref="myIonList">
<ion-item-sliding v-for="user in users" :key="user">
<ion-item-options side="start">
<ion-item-option v-on:click="favorite(user)" color="primary">
<ion-icon slot="icon-only" :icon="heartOutline"></ion-icon>
</ion-item-option>
</ion-item-options>
<ion-item :color="userState(user)">
<ion-label slot="start">{{ user.name }}</ion-label>
<ion-label slot="end">{{ user.age }}</ion-label>
</ion-item>
</ion-item-sliding>
</ion-list>
</ion-app>
</template>
<script>
import {
IonApp,
IonContent,
IonList,
IonLabel,
IonItem,
IonItemSliding,
IonItemOptions,
IonItemOption,
IonIcon,
} from "@ionic/vue";
import { defineComponent } from "vue";
import { heartOutline } from "ionicons/icons";
export default defineComponent({
name: "App",
components: {
IonApp,
IonList, …
Run Code Online (Sandbox Code Playgroud) php ×8
codeigniter ×4
apache ×1
apache2.4 ×1
controller ×1
coupling ×1
csrf ×1
email ×1
exception ×1
httpd.conf ×1
inheritance ×1
javascript ×1
mod-rewrite ×1
model ×1
mysql ×1
protected ×1
regex ×1
rewritelog ×1
smtp ×1
uml ×1
vue.js ×1