Registering a route with single pages

Permalink 1 user found helpful
Using concrete5.7 I'm trying to register some routes to my current single page controllers. In the app.php I have added

Route::register('/foo/bar/view', '\My\Custom\Controller::view');

But when I try to access the page at /foo/bar/view I get an error that says:

Argument 1 passed to PageController::__construct() must be an instance of Concrete\Core\Page\Page, none given, called in /Users/davidk/Sites/concrete5.7.4.2/concrete/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Controller/ControllerResolver.php on line 156 and defined.

Please tell me what am I doing wrong.

 
A3020 replied on at Permalink Reply
A3020
What class is Controller extending? It shouldn't extend PageController in your case.

EDIT: Oh wait, you are setting up a route for a single page? Why? This should work out of the box if you add the single page in the dashboard. If you're trying to set up a route for e.g. an Ajax call, a Route::register would make more sense, I'd say.
davidvds replied on at Permalink Reply
I'm extending the PageController and the route that the page exists on is working fine, I just want to be able to have multiple routes to the same single page. So for that I thought that's what the Route::register is for.
A3020 replied on at Permalink Reply
A3020
Hmmm, I may be mistaken but I thought the whole idea of single page is that it's accessible via one URL. I have no idea how to set up aliases for single pages.
jero replied on at Permalink Reply
jero
Assuming you're wanting to run methods in your page controller, then you do not need to register anything. If your page is at index.php/mypage, then accessing that page will run the view() method in the page's controller. If you were to add another method into the controller, say myfunction(), then index.php/mypage/myfunction would run the myfunction method. You can add as many methods as you want, and each will have its own corresponding URL. If you need the same function to run for two or more URLS, you would simply call one function from within another. A better way though is to define your method with an argument, such as myfunction($myarg = false). What that does is translate the url index.php/mypage/myfunction/myarg into a call to myfunction with $myarge set to "myarg" . You could then write code based upon the value of $myarg to do different things.
davidvds replied on at Permalink Reply
Great, I understand. But what's the Route::register for then? I came C5 from Symfony background and in Symfony you can assign multiple routs to the same controller and C5 is also using the Symfony routes system so why can't I create a route and map it to a single page controller?
jero replied on at Permalink Reply
jero
I seem to recall it was a pain to get routes working when I was developing a package recently. This is what I ended up with in my package controller.php:

use Concrete\Core\Routing\Router;
use Concrete\Core\Support\Facade\Route;
class Controller extends Package {
        public function on_start () {
                $routes = array(
                        'getlists' => 'getLists',
                        'subscribe' => 'subscribe'
                );
                foreach ($routes as $route => $method) {
                        $route = Router::route(array('ajax/' . $route, $this->pkgHandle));
                        Route::register($route, 'Concrete\Package\Mailchimp\Controller\Ajax::' . $method);
                }
        }
}


This produces routes to the URL /index.php/ccm/mailchimp/ajax/subscribe (and /getlists) and fires the subscribe (or getlists) method found in packages/mailchimp/controllers/ajax.php which defines the Ajax class.