在我的应用程序中,我有一个Controller由main方法启动的.控制器初始化钩子,数据库连接,UI,另一个连接和其他东西.它占据了程序的大部分状态(不,它不是Singleton).在另一个例子中,有一个用于机器人的控制器,用于处理解释和发出命令.两者都是相当大的文件.
我已经阅读了上帝的对象,但我真的不知道如何将它分开.如果我将机器人中的解释器和调度员分开,它将会形成一个可怕的调用链(类似于getBot().getParser().getOutput().sendMessage(recipient, message)).类似地,在第一个控制器中,如果我分开,你将只有包含字段的数据对象和一些别名实用程序方法.将它们拆分会让事情变得更糟.在你认为它不可维护之前,实际上并非如此.我甚至没有写Bot控制器,但我仍然知道发生了什么.
但问题是Bot类是2000行(如果我拿出Javadoc注释可能会更短)并且Bot大约是1000行.很多行=上帝的对象.但是对于项目的一个或两个核心类是否可以?
我正在使用PHP的CodeIgniter框架.我创建了一个名为"login.php"的视图.有一次,我
创建了视图,然后我将视图加载到名为"index"的函数中
在一个名为"CCI"的类中,它扩展了Controller,但我一直收到这个错误:致命
错误:在C:\ wamp\www\FinalP_CCI_Clone\system中调用未定义的函数site_url()
第12行的\ application\views\login.php.我不明白我的问题因为
欢迎页面加载正常,我在"CCI"类中的第二个函数加载也很好.
以下是一些代码:
控制器文件:
function CCI()
{
parent::Controller();
}
function index()
{
$this->load->view('login');
}
function test()
{
echo "Testing Data";
}
Run Code Online (Sandbox Code Playgroud)
}
/*文件结尾login.php //位置:./ system / application / controllers / cci.php*/
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('welcome_message');
}
function test()
{
echo "Testing Data";
}
Run Code Online (Sandbox Code Playgroud)
}
/*文件结束welcome.php //位置:./ system/application / controllers / welcome.php*/
想想一个简单的Rails脚手架应用程序,其中包含一个表单,用于通过"保存"按钮将记录添加到数据库中.在"创建"操作之后,控制器重定向到"显示"操作,用户可以使用"编辑"链接编辑刚刚插入的记录.到目前为止,这么简单.
但是,如果用户在创建记录后使用浏览器的后退按钮返回"新"操作,则浏览器会显示包含用户刚刚输入的值的表单.现在他改变了一些值并再次按下"保存".他认为这会改变记录,但当然这会创造一个新记录.
防止此类重复条目的首选方法是什么?我正在寻找一个通用的解决方案,可能基于cookie或JavaScript.
我有一些控制器用于特定目的,每个控制器都有一个"def索引"功能.like/index使用主页并调用未经身份验证的主页./ jobs/index是另一个,/ users/index是另一个.他们每个人都使用不同的控制器,如JobsController,HomeController,UsersController.
我的ApplicationController有一个before_Filter
before_filter :authenticate_user!, :except => [:index]
Run Code Online (Sandbox Code Playgroud)
这个问题是,当我调用/ jobs/index或/ users/index(或任何其他/*/索引页面)时,这也会跳过.有没有办法让我只能将"/ except"子句应用于"home #index"请求(即HomeController - > index page).我试过了,
before_filter :authenticate_user!, :except => ["home#index"] but it doesn't work.
Run Code Online (Sandbox Code Playgroud) 对于URL重定向,我需要在控制器中翻译某些内容.
我如何访问$ this-> translate(); 在控制器?
谢谢!缺口
当我遇到这个问题时,我正在将Laravel 3应用程序更新到Laravel 4 ...
我试过的路线:
Route::get('backend/login', 'backend/UserController@login');
Route::get('backend/login', 'backend.UserController@login');
Run Code Online (Sandbox Code Playgroud) 在单元测试中模拟以下代码的最佳方法是什么:
public ActionResult Products()
{
ViewBag.Title = "Company Product";
IEnumerable<ProductDetailDto> productList = ProductService.GetAllEffectiveProductDetails();
ProductModels.ProductCategoryListModel model = new ProductModels.ProductCategoryListModel
{
//the type of ProductDetails => IEnumerable<productDetailDto>
ProductDetails = ProductService.GetAllEffectiveProductDetails(),
//the type of ProductCategoryList => IEnumerable<selectlistitem>
ProductCategoryList = productList.Select(x => new SelectListItem
{
Value = x.FKProductId.ToString(),
Text = x.Name
})
};
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
仅供参考,我正在开发VS 2012,MVC 4.0,使用MOQ对象进行单元测试和TFS设置.
任何人都可以帮我解决这个问题,对于上述方法,使用模拟对象的最佳测试方法是什么?
我正在尝试实现一个帖子功能,并希望从php视图中选择消息和图像.我的消息功能正常.但在图像上传时我收到错误You did not select a file to upload.这是我的控制器功能
function post_func()
{
session_start();
echo $post_message=$_POST['post'];
echo $share_with=$_POST['share_with'];
echo $image=$_POST['image'];
if($image==null){
echo "<br/>no image<br/>";
}
else{
////////////////////////////////////////////////////////////////////////////////////////
$config['upload_path'] = './application/css';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo "<br/>";
echo $this->upload->display_errors();
echo "<br/> image error<br/>";
}
else
{
echo "<br/> reached <br/>";
session_start();
$this->membership_model->insert_images($this->upload->data(),$email);
$data = array('upload_data' => $this->upload->data());
echo "<br/ problem<br/>"; …Run Code Online (Sandbox Code Playgroud) 是否可以从Laravel 5中另一个控制器内的一个控制器调用一个方法(无论用于访问每个方法的http方法)?
我有一个Symfony控制器,它基本上检查请求中是否有请求的参数,然后将这些参数传递给服务.该服务使用Guzzle来调用API,对结果做一些事情然后将其传递回控制器以显示带有响应的Json.
我有一个关于错误处理的noob问题,如果我用Guzzle调用的Api返回错误,最好的解决方案是什么?
解决方案1:我是否应该使用我自己的服务中注入的Logger服务来记录错误,并将错误返回给我的控制器以显示它.
解决方案2:我应该在服务中抛出异常,在我的控制器中捕获它并在控制器中使用$ this-> get("Logger")以便在日志文件中记录错误
controller ×10
php ×3
codeigniter ×2
laravel ×2
browser ×1
crud ×1
external ×1
file-upload ×1
function ×1
god-object ×1
java ×1
laravel-4 ×1
laravel-5 ×1
logging ×1
methods ×1
monolog ×1
moq ×1
routes ×1
service ×1
symfony ×1
translation ×1
unit-testing ×1
view ×1