aaron-san / vocab-builder

GNU General Public License v3.0
0 stars 0 forks source link

creating routes #2

Open Ronin-0822 opened 2 months ago

Ronin-0822 commented 2 months ago

new routes are created in the routes.php file like this: $router->addRoute('/route', Controller, action)

The router gets a uri and calls the corresponding action.

So in case of a static route (no parameters sent) for an 'about' page... $router->addRoute('/about', AboutController::class, 'index'

How we want to structure the controllers is up to us. We could have a separate Controller for every page with an index() action, or have a MainPageController with actions for every page:

class MainPageController {

    public function home(){
        return view('Home.php');
    }

    public function about(){
        return view('About.php');
    }

So, it's basically a choice between multiple small controllers, or a single controller that might get very big.

Oh, actually, we could have just a PageController that takes a $page parameter.

class PageController {

    public function page($page) {
        return view($page . '.php');
    }
}

That's something I'm struggling a lot with at the moment, how to best structure and architect applications. What business logic goes into what Model/Controller...

aaron-san commented 2 months ago

Yeah, the MainPageController might get pretty large. I'm in favor of a separate Controller for every page with an index() action. Can we try that?

In Node, a common approach is a "controllers" directory with controllers separated by category. For example, there might be a controller/userController.js that contains all the functionality to run when the http://localhost/user route is accessed.

Ronin-0822 commented 2 months ago

Yeah, I was leaning towards the separate Controller for every page, too. Let's take that approach then.

In PHP there's a thing called 'namespaces', which allows class files being organized into directories. So, even within the Controllers we could have some subdirectories to categorize the class files even further (usually only 1 or 2 folders down). This also solves the problem of name collisions or extra long classnames.

aaron-san commented 2 months ago

That makes sense. I will try to create some routes.