CodeIgniter Forums
ci3 use controller function in "main" view. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: ci3 use controller function in "main" view. (/showthread.php?tid=75486)



ci3 use controller function in "main" view. - Goddard - 02-11-2020

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


RE: ci3 use controller function in "main" view. - dave friend - 02-11-2020

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.


RE: ci3 use controller function in "main" view. - Goddard - 02-12-2020

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?


RE: ci3 use controller function in "main" view. - dave friend - 02-12-2020

(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.


RE: ci3 use controller function in "main" view. - jreklund - 02-12-2020

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.


RE: ci3 use controller function in "main" view. - Goddard - 02-14-2020

(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?


RE: ci3 use controller function in "main" view. - jreklund - 02-14-2020

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