开发者

How can I pass variables from view to controller?

开发者 https://www.devze.com 2023-03-13 00:16 出处:网络
I have a Codeigniter vi开发者_运维问答ew which does some processing and creates some variables which I need in my controller.

I have a Codeigniter vi开发者_运维问答ew which does some processing and creates some variables which I need in my controller.

How can I access the variables created in my view from my controller? Is it even possible to do so?

I can pass variables from the controller to the view, Can I pass variables back from the view to the controller?

Just to make things more clear. I have a main controller which takes multiple views and forms a complete page. In this complete page I just need to pass some data. This data is again, don't flame or blame me for this, is processed in a view. Now I need to set up meta data, title keywords etc, for every page which is set by a main controller method setPageMetaData and it accepts a key value pair. For some pages its easy to setPageMetaData inside controller but in one particular case the title and keywords are generated by some processing in view. I need that information in my controller. My question was regardless of this particular case, can I pass variables from View to my Controller? Is it even possible? I could have added, "is it recommended?" to earn some applauses.


Think of it like this:

  • Controller = Input (including URLs)
  • Model = Processing (talks to the controller)
  • View = Output (what the users see after processing data)

Try to move your code to the appropriate layer instead of mixing it all up. Your view layer should only be receiving data from the controller and not processing it.

If you just need some static data, consider moving it to a config file. If you are processing some data, do this in the Model or Controller layer, read the result, set some variables accordingly, and send them to the view.


You can use URI segments or Js, Ajax to help too. Simple if you use CRUD it's give controller load to do code in models.


In Code Igniter if you are trying to pass something out of your view back to a controller you are doing something horribly, horribly, horribly wrong.

The only scenario for this is if your view is doing some processing itself.

Repeat after me;
I Rajendra Prasad Panchat promise I will never Do any processing in my views

This completely breaks the purpose of using a framework like Code Igniter.

The purpose of a view is to output your already processed data as a webpage.

--
Feel free to post your code and we can tell you where you are going wrong.

0

精彩评论

暂无评论...
验证码 换一张
取 消