JoshWilley's avatar

[L4] Route Collections

I'm working on a system where users are able to create pages and give those pages custom URLs.

I need to be able to protect certain URLs, however. Particularly those that I have defined in the routes.php files in my packages.

I've tried this, but it returns an empty array:

$collection = new \Illuminate\Routing\RouteCollection();

dd($collection->getRoutes());

How do I get my routes (located in various routes.php files) to register in the RouteCollection class so I can check against those routes when a user creates a page and sets the URL?

0 likes
2 replies
JoshWilley's avatar

Nevermind - Got this one solved. The problem was that I was newing up the RouteCollection class.

This appears to work:

use Illuminate\Foundation\Application;

class SomeController extends BaseController {
    
    public function __construct(Application $application)
    {
        $this->application = $application;
        dd($this->application->router->getRoutes());
    }

}
1 like
kelsen's avatar

Thanks for @JoshWilley

<?php namespace App\Http\Controllers;
use Route;
class TestController extends Controller {
    public function index()
    {       
        dd(Route::getRoutes());
    }
}

Response like this

RouteCollection {#24 ▼
  #routes: array:6 [▶]
  #allRoutes: array:45 [▶]
  #nameList: array:3 [▶]
  #actionList: array:41 [▶]
}

Please or to participate in this conversation.