Download the PHP package itcig/sagecontroller without Composer
On this page you can find all versions of the php package itcig/sagecontroller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download itcig/sagecontroller
More information about itcig/sagecontroller
Files in itcig/sagecontroller
Package sagecontroller
Short Description WordPress package to enable a basic controller when using Timber/Twig with Sage 9
License
Homepage https://github.com/itcig
Informations about the package sagecontroller
Controller
WordPress package to enable a controller when using Timber/Twig with Sage 9. This also requires heavy modification of Sage 9 to use.
Installation
Composer:
Please note that Controller is no longer an mu-plugin and is now a Composer theme depedency.
Browse into the Sage theme directory and run;
Requirements:
- PHP >= 7.0
Setup
By default, create folder app/Controllers/
within your theme directory.
Alternatively, you can define a custom path using the filter below within your themes resources/functions.php
file;
The controller will autoload PHP files within the above path and its subdirectories.
Usage
Creating a basic Controller:
- Controller files follow the same hierarchy as WordPress.
- Extend the Controller Class— it is recommended that the class name matches the filename.
- Create methods within the Controller Class;
- Use
public function
to expose the returned values to the Twig views/s. - Use
public static function
to use the function within your Twig view/s. - Use
protected function
for internal controller methods as only public methods are exposed to the view. You can run them within__construct
.
- Use
- Return a value from the public methods which will be passed onto the Twig view.
- Important: The method name is converted to snake case and becomes the variable name in the Twig view.
- Important: If the same method name is declared twice, the latest instance will override the previous.
Examples:
The following example will expose $images
to resources/views/single.twig
app/controllers/Single.php
resources/views/single.twig
Creating Components;
You can also create reusable components and include them in a view using PHP traits.
app/controllers/partials/Images.php
You can now include the Images trait into any view to pass on variable $images;
app/controllers/Single.php
Using Static Methods;
You can use static methods as a pass-thru method that returns content from your controller.
This is useful if you are within the loop and want to return data for each post item individually by passing in a $post_id.
app/controllers/Archive.php
resources/views/archive.php
Inheriting the Tree/Heirarchy;
By default, each Controller overrides its template heirarchy depending on the specificity of the Controller (the same way WordPress templates work).
You can inherit the data from less specific Controllers in the heirarchy by implementing the Tree.
For example, the following app/controllers/Single.php
example will inherit methods from app/controllers/Singular.php
;
app/controllers/Single.php
If you prefer you can also do this;
You can override a app/Controllers/Singular.php
method by declaring the same method name in app/Controllers/Single.php
;
Creating Global Properties;
Methods created in app/Controllers/App.php
will be inherited by all views and can not be disabled as resources/views/layouts/app.php
extends all views.
app/Controllers/App.php
Disable Option;
Twig Debugging;
Coming soon
Updates
Composer:
- Change the composer.json version to ^2.0.2
- Check CHANGELOG.md for any breaking changes before updating.
WordPress:
Includes support for github-updater to keep track on updates through the WordPress backend.
- Download github-updater
- Clone github-updater to your sites plugins/ folder
- Activate via WordPress
Special Thanks
- Most of the leg work here was done by Daren Jacoby so show him some love
- For Controller updates and other WordPress dev, follow @withjacoby
All versions of sagecontroller with dependencies
composer/installers Version ^1.12
symfony/yaml Version ^3.2
brain/hierarchy Version ^2.3