Advice needed to put a user_id in the middle of URL

Hi guys,

I've created an advanced module called "sportcenter" where users log their sport results.
The _construct in the "sportcenter" controller checks if the user is logged in.
If not - it redirects to the login page (Ion auth).
If yes - it redirects to domain.com/sportcenter/profile. The "profile" is a simple module in "sportcenter" as it is a table in the DB.
When a user wants to view the results goes to: domain.com/sportcenter/results. The "results" is the function in the "sportcenter" controller as it is a viewing of processed data from the DB entered by users.

Now I got an idea to create the sharing of the "results" to let everyone view it without need of registering.
In my perfect world I would like the URL to looks like this: domain.com/sportcenter/user_id/results.

Of course I will move authenticating of the user to the needed functions, but how to manage this feature the best way?
What would be the best way (also easy) for calling the "user_id" in between "sportcenter" and "results" to show the "results" page from the particular user (almost the same screen as logged in user)?

My idea is to make something like a middleware and use function index($action_word) for that.
To check if the index($action_word) in the "sportcenter" controller might act as such middleware I tested below code and calling domain.com/sportcenter/test doesn't shows the "results" page

    function index($action_word = NULL) {
        if ($action_word == 'test')
            $this->results();
        else
            redirect(SPORTCENTER_FOLDER.'/profile');
    }

    function results()
    ...

Would you be so kind and provide me any advice or tips?
Proposed URL scheme is not actually a must. Having this sharing feature without tons of code is the goal. I'm open for all the possibilities.
Maybe there is a lot easier way by doing domain.com/sportcenter/results/unique_user_name or domain.com/sportcenter/results?share=unique_user_name? But will such approach not collide with other functions in the controller?

Comments

  • edited January 2020

    just add
    function results($id) {
    $vars['profile'] = $this->my_model->find_one("id = {$id}");
    // pass $vars to view
    }
    to your sportscenter controller and have a view display the data

    url = http://domain.com/sportcenter/results/user_id

  • Hahaha, so easy peasy.
    Simplicity is the sometimes the best way out.
    Thanks!

  • edited January 2020

    Is it possible to have several functions in the controller and also accept parameters by index()?
    F.ex.:

    class Sportcenter extends CI_Controller {
    
      public function index($user_id) {
         shows the list of the tests
      }
    
      public function results($user_id, $test_id) {
         shows the results of particular test
      }
    
    }
    

    I have implemented above code and domain.com/sportcenter/results/user_id displays 404 within some generic CI template.
    Maybe I'm missing some configuration but I have checked all config and routes files and all contains the procedures accepting slugs.

  • FUEL controllers follow CodeIgniter's implementation:
    https://codeigniter.com/user_guide/general/controllers.html

    CI has a "_remap" method you can use that gets called instead of your controller methods and that way it can accept parameters.

Sign In or Register to comment.