MODX Cloud

The Most Productive MODX Learning Playground Ever

Claim Free Lab Account

Creating a Resource Class - Step 3

compared with
Current by Everett Griffiths
on Feb 21, 2013 16:15.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (10)

View Page History
h2. Creating the Resource Controllers

Okay, remember how in [Step 1|Creating a Resource Class] we told MODX where our controllers/ directory was, via the "getControllerPath" method? Well, we're going to put two files in that directory: To refresh your memory, here's the code from

First off, add create.class.php:
{code}
return $modx->getOption('copyrightedresource.core_path',null,$modx->getOption('core_path').'components/copyrightedresource/').'controllers/';
{code}

As you might have guessed, we're going to put two files into the {{core/components/copyrightedresource/controllers/}} directory. Create the directory if you haven't already and then create a file named *create.class.php*:

{code}
<?php
class CopyrightedResourceCreateManagerController extends ResourceCreateManagerController {
{code}

And add update.class.php:
Next add *update.class.php*:
{code}
<?php
{code}

And that's all we need to get our custom controllers up and running. You don't even have to include the getLanguageTopics call, but we did so we can load our custom Lexicon for the page. Any method in the ResourceUpdateManagerController or ResourceCreateManagerController can be overridden - a common one to override is "loadCustomCssJs", which would allow you to add in your own custom CSS/JS and such for your own custom UI for your CRC.
So when we're done with that, our file structure should look something like this:

!controllers.png|border=1!


And that's all we need to get our custom controllers up and running. You don't even have to include the getLanguageTopics call, but we did so we can load our custom Lexicon for the page. Read that again: you do not need to create the *getLanguageTopics()* function! You do need to create the controllers and create the classes, but you do not need to add any functions to them. If you're confused, remember our hot tip from [Part I|Creating a Resource Class]: any time you extend a PHP class, you should review the parent class that you're extending. In this case, you can take a gander at the parent classes:

* {{manager/controllers/default/resource/create.class.php}}
* {{manager/controllers/default/resource/update.class.php}}

In case you need a brush up on your object-oriented programming, any method in the ResourceUpdateManagerController or ResourceCreateManagerController can be overridden - a common one to override is "loadCustomCssJs", which would allow you to add in your own custom CSS/JS and such for your own custom UI for your CRC.

Now you can go to the Resource tree, and create a "Copyrighted Page", and it will load the Resource editing panel. Note, since we didn't override anything in the controller, it will look _exactly_ like the normal Resource editing panel. But, after you create your page and view it in the front-end, you'll note that the copyright is automatically appended:

!fe-view.png!

Wonderful! This should give you a good understanding of how resources are handled by MODX. You could stop there, but we'll go on a little bit further to describe how to [extend the processors for your CRC|Creating a Resource Class - Step 4]. That's where things get more interesting... you can customize the behavior of the manager and control where things get saved in the database, and all sorts of things...


{scrollbar}