Controllers

Controllers are the bread and butter of the framework they control when a model is used and equally when to include a view for output. A controller is a class with methods, these methods are the outputted pages when used in conjunction with routes.

A method can be used for telling a view to show a page or outputting a data stream such as XML or a JSON array. Put simply they are the logic behind your application.

Controllers can be placed in subfolders relative to the root of the Controllers folder, each class located in subfolders must have its own namespace to for instance a subfolder called Admin and a class called Users should have a namespace of App\Controllers\Admin

Controllers are created inside the app/Controllers folder. To create a controller, create a new file, the convention is to StudlyCaps without any special characters or spaces. Each word of the filename should start with a capital letter. For instance: AddOns.php.

Controllers will always use a namespace of App\Controllers, if the file is directly located inside the controllers folder. If the file is in another folder that folder name should be part of the name space.

For instance, a controller called blog located in app/Controllers/Blog would have a namespace ofApp\Controllers\Blog

Controllers need to use the main Controller; they extend it, the syntax is:

namespace App\Controllers;

use Core\Controller

class Welcome extends Controller
{ }

Also, the view class is needed to include view files, you can either call the namespace then the view:

Create an alias:

use Core\View;

Then to use:

return View::make('path);

Example

namespace App\Controllers;

use Core\View;
use Core\Controller; class Welcome extends Controller
{
public function __construct()
{
parent::__construct();
} public function index()
{
return View::make('Welcome/Welcome')->shares('title', 'Welcome);
}
}

Controllers will need to access methods and properties located in the parent controller (app/Core/Controller.php) in order to do this they need to call the parent constructor inside a construct method.

public function __construct()
{
parent::__construct();
}

The construct method is called automatically when the controller is instantiated once called the controller can then call any property or method in the parent controller that is set as public or protected.

The following property becomes available to the controller

  • $language / used to call the language object, useful when using language files

Both models and helpers can be used in a constructor and added to a property then becoming available to all methods. The model or helper will need to use its namespace while being called

namespace App\Controllers;

use Core\View;
use Core\Controller; class Blog extends Controller
{
private $blog; public function __construct()
{
parent::__construct();
$this->blog = new \App\Models\Blog();
} public function blog()
{
$posts = $this->blog->getPosts(); View::make('Blog/Posts')->shares('title', 'Blog')->withPosts($posts);
}
}

An alternative way to load a view is to call $this->getView() this acts in the same way as View::make() only without the passed params, the path is worked out internally.

To use getView the view path should follow the controller and method name ie:

class Users extends Controller
{
public function index()
{
return $this->getViews()->shares('title', 'The Title');
}
}

Will match to app/Views/Users/Index.php

Methods

To use a model in a controller, create a new instance of the model. The model can be placed directly in the models folder or in a sub folder, For example:

public function index()
{
$data = new \App\Models\Classname();
}

Helpers

A helper can be placed directly in the helpers folder or in a sub folder.

public function index()
{
//call the session helper
Session::set('username', 'Dave');
}

Load a view, by calling its render method, pass in the path to the file inside the views folder.

use Core\View;

public function index()
{
//static way
View::make('Welcome/Welcome');
}

A controller can have many methods, a method can call another method, all standard OOP behaviour is honoured. Data can be passed from a controller to a view by passing an array to the view. The array can be made up from keys. Each key can hold a single value or another array. The array must be passed to the method for it to be used inside the view page or in a template (covered in the templates section)

$content = 'The contact for the page';
$users = array('Dave', 'Kerry', 'John'); View::make('Contacts/Contacts')->withContent($content)->withUsers($users);

Using a model is very similar, an array holds the results from the model, the model calls a method inside the model.

$contacts = new \App\Models\Contacts();
$data['contacts'] = $contacts->getContacts();

Controllers的更多相关文章

  1. ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results

    原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...

  2. 【转】Controllers and Routers in ASP.NET MVC 3

    Controllers and Routers in ASP.NET MVC 3 ambilykk, 3 May 2011 CPOL 4.79 (23 votes) Rate: vote 1vote ...

  3. 控制器层(Controllers)

    本章译者:@freewind 业务逻辑代码通常位于模型(model)层.客户端(比如浏览器)无法直接调用其中的代码,所以模型对象提供的功能,必须作为资源以URI方式暴露给外部. 客户端使用HTTP协议 ...

  4. Presenting view controllers on detached view controllers is discouraged <CallViewController: 0x14676e240>.

    今天在优化app时,发现程序出现这种警告:“ Presenting view controllers on detached view controllers is discouraged <C ...

  5. [译]在AngularJS中何时应该使用Directives,Controllers或者Service

    原文: http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/ Services Servic ...

  6. How to Create Mixed Reality Videos for the Vive - with Two Controllers

    http://secondreality.co.uk/blog/how-to-create-mixed-reality-videos-for-the-vive-with-two-controllers ...

  7. 玩转单元测试之Testing Spring MVC Controllers

    玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/4311657.html The Spri ...

  8. 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers

    就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/43 ...

  9. 更轻量的 View Controllers

    iew controllers 通常是 iOS 项目中最大的文件,并且它们包含了许多不必要的代码.所以 View controllers 中的代码几乎总是复用率最低的.我们将会看到给 view con ...

  10. 【IOS笔记】Resource Management in View Controllers

    Resource Management in View Controllers 视图控制器的资源管理 View controllers are an essential part of managin ...

随机推荐

  1. 【转】Android中removeCallbacks失效原因

    原文网址:http://blog.sina.com.cn/s/blog_6714fba70100wtx1.html 在Android开发中会使用Handle的removeCallbacks函数,该函数 ...

  2. 学习面试题Day06

    1.字节流的处理方式 字节流处理的是计算机最基本的单位byte,它可以处理任何数据格式的数据.主要的操作对象就是byte数组,通过read()和write()方法把byte数组中的数据写入或读出. 2 ...

  3. NGINX开篇

    前言 最近空闲时间比较多, 开始阅读nginx源码, 阅读的过程总结和笔记整理了下, 汇集成了一个系列的文章, 由于nginx功能实在太多, 没法做到面面俱到, 只对已经阅读过的源码进行记录总结, 以 ...

  4. iOS程序性能优化

    iOS程序性能优化 一.初级 使用ARC进行内存管理 在iOS5发布的ARC,它解决了最常见的内存泄露问题.但是值得注意的是,ARC并不能避免所有的内存泄露.使用ARC之后,工程中可能还会有内存泄露, ...

  5. 初识Ajax---简单的Ajax应用实例

    原文: http://www.ido321.com/347.html 从网页前端输入提示范围内的字符,然后显示从后台返回的结果 1: <html> 2: <head> 3: & ...

  6. 一键生成HTML4和WAP站

    前两天在QQ空间上提到微信上线风铃时,把其中的HTML5错打成HTML4,结果发现很多媒体微博照着转载,依旧说成 "一键生成HTML4和WAP站",这就是转载不注明出处.不署名.不 ...

  7. Pritunl:简易搭建个人VPN及年费200的超编译独立主机 BandwagonHost

    https://pao-pao.net/article/213 Pritunl:简易搭建个人VPN 文/ Vergil 一 直以来安装 VPN 服务.提供全局加密代理,是租用VPS(虚拟主机)的一个重 ...

  8. 射频识别技术漫谈(5)——防冲突【worldsing 笔记】

    正常情况下读写器某一时刻只能对磁场中的一张射频卡进行读写操作.但是当多张卡片同时进入读写器的射频场时,读写器怎么办呢?读写器需要选出唯一的一张卡片进行读写操作,这就是防冲突. 防冲突机制是非接触式智能 ...

  9. opencv 图像轮廓

    图片解析: 原图: code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\c ...

  10. 关于UIImage类的对象两种初始化方法的区别

    1.imageNamed: UIImage *image = [UIImage imageNamed:"]; UIImage的类方法 第一次读取图片的时候,先把这个图片放到缓存中,下次再使用 ...