Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do the following #214

Open
ErikThiart opened this issue Oct 5, 2023 · 4 comments
Open

How to do the following #214

ErikThiart opened this issue Oct 5, 2023 · 4 comments

Comments

@ErikThiart
Copy link

$router->get('/', 'index.php');
$router->get('/about', 'about.php');
$router->get('/contact', 'contact.php');

$router->get('/notes', 'notes/index.php')->only('auth');
$router->get('/note', 'notes/show.php');
$router->delete('/note', 'notes/destroy.php');

CleanShot 2023-10-05 at 11  54 03

@ErikThiart
Copy link
Author

I know the routers and screenshot do not match, ignore that. Conceptionally I want to understand how I can send the route to a specific PHP file in the controller.

example

$router->get('/user', 'User/index.php');

@ErikThiart
Copy link
Author

This is how I currently do it, but it feels... dirty.

// Define routes
$router->get('/', include_once '../App/Controllers/User/index.php');

@uvulpos
Copy link
Collaborator

uvulpos commented Oct 18, 2023

Hi Erik,
a router normally just calls Controller functions / classes that are inside a file, not a file itself. That's what MVC (Model View Controller) is.

  • Model -> Database
  • View -> Template Engine like Mustache, Twig etc
  • Controller -> App Logic

👉🏻 https://www.freecodecamp.org/news/the-model-view-controller-pattern-mvc-architecture-and-frameworks-explained/

to import the file there are two ways:

  1. when the app is tiny, you can for learning purposes include all your controller files in the router function, but I will take a lot of performance, since php will read them all in with dependencies
  2. you take a look into autoload. Composer also uses autoload already, and you can just include it. Autoload means PHP knows itself when to include which file when and only if needed and called (preferred solution)

Youtube How to autoload:
https://www.youtube.com/results?search_query=autoload+composer

@uvulpos
Copy link
Collaborator

uvulpos commented Oct 18, 2023

Maybe this helps to see, besides passing the function by name / reference you can also define an anonymous function in it itself (shown here, but rather quick and dirty)
https://github.com/bramus/router/blob/master/demo/index.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants