Moving from Tools to Routes

I'm trying to map a route to a class I used for a tool in 5.6. How to do that exactly? I'm using this:

Route::register('/mypackage/campaign', '\Concrete\Package\Mypackage\Controllers\CampaignController');

But accessing /index.php/mypackage/campaign gives me this error message:

Class 'Concrete\Package\Mypackage\Controllers\MypackageController' not found

Note that this isn't a real c5 controller since it was previously a tool, I'd like to use it anyway though, and thought it could be done with the new Routing. Can anyone shed light on how to do this?

View Replies: View Best Answer
mkly replied on at Permalink Best Answer Reply
Since a tools file was pretty much just an include you could probably just do something like this in the short term.
Route::register('/mypackage/campaign', function() {
ConcreteConversion replied on at Permalink Reply
Ah, nice and simple, thanks! (And turning on full error display helped me further, my bad.)

While I'm at it, do you know how the namespace routing works with the core classes? I don't understand exactly how you can "use [Any core class];" anywhere and it works?
mkly replied on at Permalink Reply
concrete5.7 uses PSR-4(slightly modified) for autoloading. Basically that means that you can line up the classes you see with the /concrete/src/ directory being '\Concrete'

use Concrete\User\User;

Would correspond with the file

But since concrete5.7 also uses class aliases for many of the core classes you can also just use
use User;

To see which classes have aliases assigned to them you can look here

Both of the above `use` statements will work, the shorthand aliases are just there for simplicity and convince.
jgarcia replied on at Permalink Reply
I'm in the same boat, but don't quite follow how this needs to be setup. I previously could access tools within my package via /tools/packages/{package-name}/{tool-name}. As a matter of fact, this is still the URL that is generated by C5 when using:

<?php echo Loader::helper('concrete/urls')->getToolsURL('{tool-name}','{package-name}');?>

Yet, when I access this URL in 5.7, I just get a blank page. My tool consists of nothing but

echo 'Hello world.';

Am I missing something? If I need to register a route, where do I do so within the package?
jgarcia replied on at Permalink Reply
ConcreteConversion replied on at Permalink Reply
Ah great, one of the replies there looks like a good way to use controllers:
ConcreteConversion replied on at Permalink Reply
Yes you need to define routes for this to work. A nice place to do that is in the package controller, since it is instantiated automatically when the package is installed. Something like this:

namespace Concrete\Package\YourPackage;
use Package;
use Route;
use View;
class Controller extends Package
  // ... further down ...
  public function on_start() {
    Route::register('/some/nice/url', function() {
      View::element('viewName', null, 'your_package');

The above code will display viewName.php which is supposed to be in the "elements" directory in your package.

Since you'll request this as a URL, it will probably be nicer to use a concrete controller instead, creating it inside the anonymous function. Haven't got an example how to do that, it should be much easier to do with 5.7 than before though.

EDIT: This looks like a good way to do it: