Skip to content

Instantly share code, notes, and snippets.

@deuterium7
Last active August 22, 2017 17:11
Show Gist options
  • Save deuterium7/cf1471aeaae7d75f4a566b411194721e to your computer and use it in GitHub Desktop.
Save deuterium7/cf1471aeaae7d75f4a566b411194721e to your computer and use it in GitHub Desktop.

autoload/global.php

<?php

/**
 * Global Configuration Override
 *
 * You can use this file for overriding configuration values from modules, etc.
 * You would place values in here that are agnostic to the environment and not
 * sensitive to security.
 *
 * @NOTE: In practice, this file will typically be INCLUDED in your source
 * control, so do not include passwords or other sensitive information in this
 * file.
 */
return [
    'db' => [
        'driver' => 'Pdo',
        'dsn' => "mysql:host=localhost;dbname=blog;charset=utf8",
        'user' => "root",
        'password' => "root"
    ],
];

module.config.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Blog;

use Zend\Router\Http\Literal;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'blog' => [
                'type' => Literal::class,
                'options' => [
                    'route' => '/blog',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action' => 'index',
                    ],
                ],
            ],
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            __DIR__ . '/../view'
        ],
    ],
];

Post.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Blog\Model;

/**
 * Description of Post
 *
 * @author group1-16
 */
class Post {

    public $id;
    public $title;
    public $content;
    public $status;
    public $dateCreated;

    public function exchangeArray(array $data) {
        $this->id               = !empty($data['id']) ? $data['id'] : null;
        $this->title            = !empty($data['title']) ? $data['title'] : null;
        $this->artist          = !empty($data['artist']) ? $data['artist'] : null;
        $this->content       = !empty($data['content']) ? $data['content'] : null;
        $this->status         = !empty($data['status']) ? $data['status'] : null;
        $this->dateCreated = !empty($data['date_created']) ? $data['date_created'] : null;
    }

}

PostTable.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Blog\Model;

use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;

/**
 * Description of PostTable
 *
 * @author group1-16
 */
class PostTable {

    private $tableGateway;

    public function __construct(TableGatewayInterface $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll() {
        return $this->tableGateway->select();
    }

}

Module.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Blog;

use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

/**
 * Description of Module
 *
 * @author group1-16
 */
class Module implements ConfigProviderInterface {

    //put your code here
    public function getConfig() {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function getServiceConfig() {
        return [
            'factories' => [ Model\PostTable::class => function($container) {
                    $tableGateway = $container->get(Model\PostTableGateway::class);
                    return new Model\PostTable($tableGateway);
                },
                Model\PostTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Post());
                    return new TableGateway('post', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }

    public function getControllerConfig() {
        return [
            'factories' => [
                Controller\IndexController::class => function($container) {
                    return new Controller\IndexController($container->get(Model\PostTable::class));
                },
            ],
        ];
    }

}

IndexController.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Blog\Controller;

use Blog\Model\PostTable;
use Zend\View\Model\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;

/**
 * Description of IndexController
 *
 * @author group1-16
 */
class IndexController extends AbstractActionController {

    private $table;
    
    public function __construct(PostTable $table) {
        $this->table = $table;
    }
    
    //put your code here
    public function indexAction() {
        return new ViewModel([
            "posts" => $this->table->fetchAll()
        ]);
    }

}

index.phtml

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
foreach ($posts as $post) {
    echo $post->title."<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment