txsframework/framework

There is no license information available for the latest version (0.1.5) of this package.

An mvc framework

0.1.5 2014-06-09 22:23 UTC

README

Txsframework

This framework is built only for my personal learning purposes, and should not be used or moved into production.

Requirements

  • PHP >= 5.4
  • Composer

Composer installing

Download and install composer, for instructions go to https://getcomposer.org/download/

Installation

After installing composer, open terminal and run

	composer create-project txsframework/framework

This will install txsframework and all its dependencies.

OR download zip archive and..

cd into folder and run

	composer install

If composer fails or you dont want to install composer ( wierd )

As a last resort, You can download this archive https://www.dropbox.com/s/zsedwpg54bnpye2/framework.zip which has the latest version dependencies installed.

Warning

This will make all the composers autoloading classmap to fail. If you want to add a new controller or a model inside those folders. You will need to manually update composers autoloading files. This is what composer dump-autoload does for you, if you run composer. So i do recommend you to use composer if possible.

and then..

Start an PHP server from rootfolder and you are done!

php -S localhost:8000 // Using php build in server. As of php >= 5.4

When moving into production mode, at your own risk, you need to change

	$txs->setEnvironment($production = false); to => $txs->setEnvironment($production = true);

Autoloading

Txsframework is utilising composers autoloading class. What this means is that all files are automatically required once it is needed.

There are two sort of autoloading. In the main folder structure txsframework is autoloading two folders. controllers and models Whenever you create a new controller och model class file. You need to tell composer to update the registry. In terminal you type in composer dumpautoload -o. Now composer has updated its registry and the new files is autoloaded aswell.

The other way of autoloading is by defining a namespace and where those files are located. If you should develop a plugin to txsframework, to autoload the files you neeed to specify psr-4 block. Read more on https://getcomposer.org/doc/01-basic-usage.md#autoloading

Routing

If in development mode you can append ?routes to see a list of registred routes in the browser. See above on how to change development modes.

	<?php
	
	/*  
	# You can route to classes, specifying the method after @-sign
	# The framework doesnt care about naming conventions of controllers
	# It does care about where to look, default is controllers folder
	# You can specify your own folders in composer.json file.
	*/ 
	
	$app['router']->get('/', 'HomeController@index');
	
	/* 
	# You can use closures instead of controllers, be sure to pass $app into the 	# closure so that you can access all feautures
	*/
	
	$app['router']->get('/logout', function() use($app){
		$user = '';
		
		if($app['session']->has('user')) {
			$app['session']->unset('user');
		}
		
	});

Request

Request class can answer on methods like:

* is*
* isGet()
* isPost()
* isPut()
* isDelete()

Request class has access to post-data request payloads.

In a post-request with a payload
	e.g 
	"name": "txs"
You can access data using
	$app['request']->input->get('name'); // returns null if data is not present.

Every global array is wrapped with a blob class called simpleclass. It makes it easier to interact with the global variables using simple methods. For example you can access server globals array using:

$app['request']->server->get()

Following methods is availble in SimpleClass

* ->get($key = null) // if null it returns the array itself. 
* ->set($key, $value)
* ->has($key)
* ->remove($key)

Response

Txsframework can handle both custom response objects or serve a view.


	<?php
	
	class HomeController {
		
		public function __construct(App $app) 
		{
			$this->app = $app;
		}
		
		
		public function index() 
		{
			$user = $this->app['session']->get('user');
			
			// Headers and statuscode defaults to text/html and 200 statuscode.
			return $this->app['response']->build('<h1>Hello $user</h1>', ['Content-Type' => 'text/html', 200]);
			
			// Or if you want to serve a view, either use view instance or use response view method
			return $this->app['response']->view('front/index');
			
			// Or return json if you want.
			return $this->app['response']->json(['name' => $user]);
			
			// return custom response
			return $this->app['response]->build('Something went wrong', ['Content-Type' => 'text/plain'], 500);
			
			// You can also redirect
			$this->app['redirect']->to('/');
			
			
		}
	
	} 

View/Templating

View can render template files, that lies inside views folder. View path can be changed in start.php

View can render a template with the render method. Type in the path to the template file. File templates must emit the txs.php file extension. Inside render method path to template, ommit .txs.php


		return $this->app['response']->render('welcome');			

Views partials and master layouts

Sometimes you want to reuse a template on multiple pages. Then you can have your views extend a master template layout.

 // shared/master.txs.php
	<!doctype html>
	..
		<body>		
		<h1> My master layout </h1>
		<div class="sidebar">
			##insert('sidebar')
		</div>
	   <div class="main">
	   	##insert('main')
	   </div>
		</body>

Extending a master layout

	// a partials that wants to extend a master with sections, needs to declare the sections.
	##extends('shared/master')
	
	##section('sidebar')
		<ul>
			<li><a href="/contact"></a></li>
		</ul>
	##end
	
	##section('main')
		<h3> main </h3>	
	##end 

Container

Txsframework is using an IoC-container to resolve classes inside the framework. This makes it alot easier to write unit tests. The core classes is registred in Environment class and is made into singletons.


	// You can register your aliases inside the container.
	$app->register('user', function() {
		return new User();
	});
	
	// You can make it a singleton by setting the last parameter to true.
	// The user will then return the same instance throughout the request.
	$app->register('user', function() {
		return new User();
	}, true);
	
	$app->resolve('user'); // Resolve the instance from the container.
	
	// This has no alias registered, so the container will try to resolve it with reflection api. And it will try to resolve any dependecies aswell.
	$app->resolve('\Acme\Foo'); 
	

Syntax sugar.
You can interact with $app as an array. Through php interface ArrayAccess

	$app['user'] is the same as $app->resolve('user');

Providers

Inside Environment class you can register providers that you want to prepare the framework with.

Controllers

Controllers are located in app/controllers. Composer will autoload every class inside that folder. If you want to load from another folder, you can change composer.json and add the folder inside classmap. Dont forget to run composer dump-autoload -o in the terminal after you added a new path.

The container will try to inject any dependencies inside the constructor, so if you to inject the User model class.


		public function __construct(User $user);

And the container till inject it for you.

Db BETA. Limited functionality

Inside app/config/application.php you can change your choosen db connection. As of now, sqllite is the only supported database

Db model

You can add a database model to app/modeldirectory. The name of the model need to match the table name in the db and needs to extend \Txs\Db\Db()


	<?php 
	use Txs\Db\Db;
	
	class User extends Db {
		// and you are done.
	}
	
	
	Now resolve user from the ioc or autoinject it through the constructor.
	
		$user = $this->app['User'];

		$user->firstname = "Jane";
		$user->lasttname = "Doe";
		$user->email     = "jane.doe@doe.com"
		$user->save();
		$id = $user->getLastId();
		
		return $this->app['redirect']->to("/user/$id"));
		
		
		$users = $this->app['User']; // ioc is resolving with ArrayAccess. Read more in #container section.
		
		// Db model always returns the result inside a Loda class // read more http://www.github.com/enpere/loda
		
		$users = $users->filter(function($user){
			return ($user->id % 2 === 0);
		});
		
		return $this->app['response']->json($users);
		

Session

You can set and get values from session class.

	$app['session']->set('year', '2014');
	$app['session']->get('year');

You can also flash message for the next request only

	$app['session']->flash('success' 'added successfully');

Roadmap

  • Cookie support
  • View template operators and conditionals.
  • Ioc container needs some optimization
  • Unit tests
  • Better documentation
  • Routing filters.
  • Event support
  • Logging
  • Custom error page.