Bundles

From AV Scripts Wiki
Jump to: navigation, search

Bundles allow for features and functionality to be added to AVCMS in a way that means they can be plugged-in and out without affecting other parts of the system. For example, the Blog bundle contains all the features for the AVCMS blog system including the core functionality, templates, assets, routes, urls, forms and settings. By default AVCMS looks for bundles in 3 directories:

src/AV/Bundles - Core framework features like the database and forms
src/AVCMS/Bundles - AVCMS functionality like the admin panel and features like the Blog
src/AVCMS/BundlesDev - Special dev features only enabled in Developer Mode

Creating a Bundle and a New Page

It is recommended you use an IDE such as PHPStorm or Netbeans to develop with AVCMS as it's a complex system and the code completion is invaluable.

Firstly, make sure AVCMS is in Developer Mode.

Then visit yoururl.com/dev/bundles/new

On this page give a name to your new bundle and leave the two other fields as default. The bundle name should have no spaces and ideally use capital letters for the first words. Examples: FacebookConnect, Games, NewComments

This will generate a basic structure for your new bundle in the src/AVCMS/Bundles folder.

You can now enable your bundle in the admin panel in the "Bundles" section.

Next, you will need to create a controller. Create a new file in your bundle's Controller directory called YourControllerName.php. The file name MUST be the same as the class name contained within. The contents of the file should look something like this:

<?php

use AVCMS\Core\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class YourControllerName extends Controller
{
    public function yourActionName(Request $request)
    {


        return new Response('THE HTML OUTPUT GOES HERE');
    }
}

So YourControllerName must match the name of the file you created otherwise the class will not be loadable. You may also want to choose a name for yourActionName, this can be whatever you want. Within the yourActionName method can be any PHP code you want to run, with the method returning a Response object with your HTML output.

Finally you will need to create a URL to this new page, so open up the generated routes.yml file in your bundle. In there, add something like this:

your_route_name:
  path
: /the-path-to-your-page
  defaults
: { _controller: YourBundleName::YourControllerName::yourActionName }

You will need to replace the various parameters with your own names that you've chosen:

 your_route_name - The name you want to give the route. Should be lowercase and use underscores instead of space.
 /the-path-to-your-page - This is the actual URL that your new page will be accessible from
 YourBundleName, YourControllerName, yourActionName - The names of the bundle, controller and action that you have created

Useful methods within the action

Within your action, you can access a few useful methods such as:

Check if the user is currently logged in:

 if ($this->userLoggedIn()) {
     // code to only run when a user is logged in
 }

Get the current user:

 $user = $this->activeUser();