• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ci3 use controller function in "main" view.

#1
I have a main view and I would like to use a controller method in it.  Is that not possible?

The controller method is an extension of CI_Controller..a MY_Controller class.

Thanks
Reply

#2
Not sure I understand your question, but as a rule functions (such as those found in a controller) should not be executed in a view. Following the "rules" of MVC, the result of any function required in a view should be obtained in the controller and the results passed to the view.

Forgive me if I have misunderstood your question.
Reply

#3
We can use controller methods from code igniter inside a view, but in the case of the "main template" file in ci3 you cannot use a controller method? At least in my testing it wasn't working.

If we are checking permissions to see if a user can view a link for example the controller has a $this->checkPermission()...it makes sense to call a function like this to verify the users permissions, no?
Reply

#4
(02-12-2020, 05:56 AM)Goddard Wrote: If we are checking permissions to see if a user can view a link for example the controller has a $this->checkPermission()...it makes sense to call a function like this to verify the users permissions, no?

Using only PHP there is no way to stop a link's action after it is clicked. When a link is clicked the browser goes immediately to the URL. The URL is represented by a controller and it is that controller which checks permissions. The user will get one view or another, or be redirected somewhere else depending on the permissions granted.

But a better use of checkPermission() might be to not show users links to pages they don't have permission to see.
Reply

#5
Personally I would pass down for e.g. Article and use $article->canEdit() for displaying the edit button in the view. And letting the class itself handle what you can do with it.
Reply

#6
(02-12-2020, 01:12 PM)jreklund Wrote: Personally I would pass down for e.g. Article and use $article->canEdit() for displaying the edit button in the view. And letting the class itself handle what you can do with it.

Yeah I do like that method.  So then when my model is initialized it would be assigning the permissions for this user?
Reply

#7
That or you need to pass down a variable that's true/false depending on your checks.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.