2

I am using cakephp 3.x have put database queries in model in which i want to check the current controller action and based on that i will make my database queries.

I know i can pass controller action from my controller itself to my model function but is there a way i can just check my current controller action inside my model only so that i do not need to pass it in my multiple functions which are inside model only.

My Try -- UsersTable.php

    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->table('users');        
        $this->primaryKey('user_id');
        echo '<pre>';
        print_r($GLOBALS);        
        exit;
    }

so far if i do this, i got an array in response and in which i found this the best result out of other things in that array

[REQUEST_URI] => /5p_group/users/add

I m also trying to use this

echo '<pre>';
print_r($GLOBALS['_SERVER']['HTTP_REFERER']);        
exit;

which gives me this output

http://localhost/5p_group/users/archived

so eventually i am getting the result which i want but i want another proper method which cakephp 3.x uses ..

So is there any other way or more frequent way from that i can get my current controller action ?

Any ideas will be appreciated ..

Thanks

Mittul At TechnoBrave
  • 1,142
  • 3
  • 25
  • 70
  • 7
    That's a clear violation of separation of concerns, ie a very bad idea, your model is not supposed to know about the outside world and make such decisions! If a controller wants data from a model, then it tells the model what data it wants, not the other way around! You may get better help if you elaborate on _what exactly_ you need to query _when exactly_. – ndm Oct 04 '16 at 06:00
  • i know it does not make sense .. but is it wrong to know the stuff ? – Mittul At TechnoBrave Oct 05 '16 at 04:43
  • It's never wrong to know about stuff, no, like bad practices and how to avoid them. And if you want to know even more, like how to do it the right way, then please elaborate on the actual technical problem that you are trying to solve, ie als already mentioned, "_what exactly_ do you need to query _when exactly_?". – ndm Oct 05 '16 at 05:30
  • I agree with you for this statement and it makes sense to me. Thanks – Mittul At TechnoBrave Oct 05 '16 at 05:46

2 Answers2

5

First of all you should use the routing class in your model. So at the top you can call it like below

use Cake\Routing\Router;

And then in your initialize function you can check the params like

debug(Router::getRequest());

Or to be more specific you can use

debug(Router::getRequest()->params['action']);

I think this is the solution using cakephp classes only, though a small hack.

For more function reference you can access the cookbook at Router Class

Sevvlor
  • 560
  • 1
  • 7
  • 24
Rohit Ailani
  • 910
  • 1
  • 6
  • 19
  • 1
    This is a really bad idea, see ndm's comment above. Model's should have no knowledge of the controller/route as they are not necessarily accessed this way. This is a bad design pattern! – drmonkeyninja Oct 04 '16 at 08:15
  • 1
    Yes, I agree but as the question simply states that he can pass the values from controller but still wants to know is there a way around. **I know i can pass controller action from my controller itself to my model function but is there a way i can just check my current controller action inside my model only so that i do not need to pass it in my multiple functions which are inside model only.** - @mittul – Rohit Ailani Oct 04 '16 at 08:43
  • 2
    Just highlighting that this should never be done for anyone stumbling across this page. This is a very hacky solution as `Router::getRequest()->params['action']` may not exist within the model when the method is called. Models are not dependent on controllers like this answer is. – drmonkeyninja Oct 04 '16 at 08:48
  • 1
    I already said that for using this you need to have this line at the top **use Cake\Routing\Router;** And also I told that this is a hack. So, I clarified myself and already this was based on specific need by the user. This should not be done otherwise. – Rohit Ailani Oct 04 '16 at 08:54
  • @mittul can you please clarify, did this serve your purpose. – Rohit Ailani Oct 04 '16 at 09:41
  • @RohitAilani yea . – Mittul At TechnoBrave Oct 05 '16 at 04:44
  • ok @MittulAtTechnoBrave please select my answer in that case and drmonkeyninja I suppose that things are clear now, that this was for a specific purpose which is not recommended otherwise. – Rohit Ailani Oct 05 '16 at 04:53
0

The question was about a Model, but it shows up on Google.

When inside a view (.ctp file), use

\Cake\Routing\Router::getRequest()->getAttributes();

CakePHP 3

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57