Controllers

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();
时间: 2024-10-18 10:04:46

Controllers的相关文章

Controllers, Actions 和 Action Results

Controllers, Actions 和 Action Results 原文:Controllers, Actions, and Action Results作者:Steve Smith翻译:姚阿勇(Dr.Yao)校对:许登洋(Seay) Action 和 action result 是开发者使用 ASP.NET MVC 构建应用程序的基础部分. 什么是 Controller 在 ASP.NET MVC 中, 控制器( Controller )用于定义和聚合操作(Action)的一个集合.操

ngLazyLoad——让ng项目实现controllers按需异步加载

最初的源码来自:https://github.com/atian25/angular-lazyload/ 但由于一些个人的原因(我有强迫症...)所以把代码的书写风格按照我平日的喜好修改了下 也顺便认真的阅读了一遍源码,按照自己的理解,把原来的英文注释替换成中文的了 毕竟不是原作者,对ng框架也不是特别的熟,注释中的用词可能有不少不正确的地方,欢迎指出 下面是我稍微修改了一下的代码: /* * angular-lazyLoad * 一个angular项目的按需异步加载服务模块 * 支持 [Sea

presentation Controllers的使用

presentation Controllers的使用 by 伍雪颖 @interface ViewController ()<UIPopoverPresentationControllerDelegate, UIAdaptivePresentationControllerDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)show:(id)

Laravel Controllers

Basic Controllers Instead of defining all of your route-level logic in a single routes.php file, you may wish to organize this behavior using Controller classes. Controllers can group related route logic into a class, as well as take advantage of mor

如何将Windows AD的Default Domain Policy和Default Domain Controllers Policy恢复

根据微软的最佳实践我们在创建AD Group Policy的时候,不建议去更改Default Domain Policy和Default Domain Controllers Policy这个两个策略中的配置.那么在不小心的情况下更改了这两个策略的配置的时候如何恢复默认设置呢? 微软提供了一个工具dcgpofix去重置这个两个策略Default Domain Policy和Default Domain Controllers Policy.(具体可以参考:https://technet.micr

part 2 Angular modules and controllers

What is a module in AngularJS? A module is a container for different parts of your application i.e controllers,services,directives,filters,etc. You can think of a module as a Main() method in other types of applications. How to create a module? Use t

[AngularJS - thoughtram] Exploring Angular 1.3: Binding to Directive Controllers

The post we have: http://www.cnblogs.com/Answer1215/p/4185504.html gives a breif introduce about bindToController on the directive. Here is a blog about bindToController from thoughtramwhich explains in detail why bingToController comes into handy an

【IOS笔记】Creating Custom Content View Controllers

Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of your app. You use them to present your app’s unique content. All apps need at least one custom content view controller. Complex apps divide the workl

laravel 在controllers中添加子文件夹和控制器出错解决办法

首先我们在controllers文件夹中建立一个admin文件夹. 第一种方法:直接在后台建立控制器,比如AdminController.php 里面正常的写上我们的内容. 在路由表中, Route::get('admin','[email protected]'); 我们发现程序报错,说无法找到控制器AdminController. 我们打开命令行工具,进入到该项目的更目录中,也就是artisan,composer.json所在的目录中, 运行命令 composer dumpautoload

更轻量的 View Controllers

View controllers 通常是 iOS 项目中最大的文件,因为它们包含了许多不必要的代码.所以 View controllers 中的代码几乎总是复用率最低的.我们将会看到给 view controllers 瘦身的技术,让代码变得可以复用,以及把代码移动到更合适的地方. http://tang3w.com/translate/objective-c/objc.io/2013/10/22/%E6%9B%B4%E8%BD%BB%E9%87%8F%E7%9A%84-view-control