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

Router parameter #1

Open
aliberkyurtoglu opened this issue Oct 21, 2020 · 8 comments
Open

Router parameter #1

aliberkyurtoglu opened this issue Oct 21, 2020 · 8 comments

Comments

@aliberkyurtoglu
Copy link

Hey there,

user/update/1 or product/{:slug} or category/{:slug}/{:id}

When we define a route in the form of, we cannot access the '1' or 'slug' parameter other than using the $ _GET global.
We send Request and Response classes as parameters to the controller. Likewise how can we send this '1' or 'slug' parameter?

@khairnajdawi
Copy link

khairnajdawi commented Sep 7, 2021

It might be a year after the question, but it worth sharing the way I did it (using preg_match)
first : index.php

  • add the route as this
    -- $app->router->get('#^/Customers/(\d+)$#',[CustomersController::class,'getCustomerProfile']);

second : Router.php

  • add array of params to the class : array $params = [];
  • in resolve method, if $callback==false add this code
    --
    $route_found = false;
    foreach ($this->routes[$method] as $path1=>$route1) {
    if (preg_match($path1, $path, $matches)) {
    $callback = $this->routes[$method][$path1];
    $this->params = $matches;
    $route_found = true;
    break;
    }
    }
    if(!$route_found){
    throw new NotFoundException();
    }

third : in controller add third parameter to your method as array
public function getCustomerProfile(Request $request, Response $response, array $params){
$customer_id = $params[1]; // do not use 0 it will be the path
// check if not null and do your thing
}

@virtual-designer
Copy link

An update to the project was made by @thecodeholic. Check out #5

@virtual-designer
Copy link

I built my own PHP MVC framework; inspired by this project. Thank you @thecodeholic

@thecodeholic
Copy link
Owner

Amazing @virtual-designer.
Love to hear such stories. Thank you

@virtual-designer
Copy link

Amazing @virtual-designer. Love to hear such stories. Thank you

Thanks sir! Here is my project link: https://github.com/onesoft-sudo/invention-project

@thecodeholic
Copy link
Owner

You got your first star from me. I will explore the project as soon as I have time.
Cheers...
Don't call me sir. Call me Zura. :)

@virtual-designer
Copy link

@thecodeholic Ok Zura. But you're a great man. You made my day :)

@AngeArsene
Copy link

I built my own PHP MVC framework; inspired by this project. Thank you @thecodeholic

I am trying to do that too. Its really cool to have such materials to work with

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

5 participants