This Tutorial is for giving you a quick overview about the most important components of the core framework.
Requires the following knowledge:
- TUTORIAL - APP BOOTSTRAPPING
Requirements
- Context Backing Class
- View/Output Code
- Context Configuration (Visibility/Access)
Files to be touched
- /backend/class/context/your-context.php (create)
- /frontend/view/your-context/your-view.php (create)
- /config/app.json (modify/create)
Step 1: Create your backing class
Create a new PHP file in backend/class/context/. This is your "context" (you can think of it as a controller).
<?php
namespace codename\demo\context;
use codename\core\exception;
/**
* sample context
*/
class mycontext extends \codename\core\context {
/**
* some description
*/
public function view_myview() {
// ... do stuff.
$this->getResponse()->setData('mykey', 'myvalue');
}
}NOTES:
- Make sure you're using the correct namespace (e.g. namespace codename\demo\context if your app is called "demo")
- Make sure your class is inheriting from a context class (e.g. \codename\core\context )
- Make sure you prefix all your view functions with view_
- You may omit the PHP closing tag (some modern-stylish PHP programming stuff...)
- Don't you ever dare to use camelCasing for context backing class files
Step 2: Create your view code
Create a new PHP file (depending on your preferred templating engine) at frontend/view/your-context/your-view.php This may be the raw HTML/PHP-Inline code of your view. If you're using Twig for templating/writing views, it may be called your-view.twig
<?php namespace codename\demo;?>
<p>Some output code</p>
<p>Get a value from the response: <?= app::getResponse()->getData('mykey') ?>NOTES:
- The example is a bare inline PHP code
- Don't forget to namespace the code if you're using *.php files (irrelevant if you're using Twig)
- Then, you can access the response constainer via app::getResponse()->getData( ... );
Step 3: Allow your view to be accessed
Open your app configuration at config/app.json. Under the key "context" create a json object declaring your context outline:
{
...
"context": {
"mycontext": {
"defaultview": "myview",
"view": {
"myview": {
"public": true
}
}
},
...
}
}NOTES:
- Required keys for each context: defaultview view
- Set "public" : true for a view to be accessed without authentication. This is fine for testing purposes.
- Optional keys: "type": optionally, you can define "crud" or another inheritable context type. Then, you might not need to define stuff that is already present in the base context type. "template": explicitly use a template here "defaulttemplateengine": explicitly use a template engine defined in environment.json here
Step 3.5: Test!
Fine, now you're good to go! Fire up your browser at http://your.url/?context=mycontext&view=myview (or even leave the view parameter, as you've defined the default view in your app.json).