• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use multiple mvcs together

#1
Hi!

Lets say I like to create a crud for a table articles.
So, I create Controller, Model and views for my articles.

I display all articles in a view loaded from  index() method.

However, in this view I also like to display a crud for stores (to interact with table stores)
So, I load stores model in articles/index() and get all stores that I send to the view along wiht the articles.

But, like I said, stores should also be a crud.
So from the view loaded from articles controller I have add, edit and delete links for stores.

I let these links go to stores controller. In the "add" case  the stores controller calls its own add() that loads a view with a form that let the user add a store. It is posted, validated and inserted from stores controller. When ready I like to redirect to articles/index(). 

The stores controller don't know where to redirect.

So, is it ok to add the return path in the article view?
Something like stores/add/artices/index

And then accept the arguments in stores controller like:

add($return_controller, $return_method){}

Then I need to send these varaibles to the stores form and then back to the stores controller to be able to finaly make my redirect:

redirect($return_controller.'/'.$return_method);

Is this a good way of doing it?
Or should I just keep everything in my articles MVC?
Or should I try HMVC? (Can't find any tutorial for CI3 that handles crud methods)
Reply

#2
For the CRUD methods you can create a MY_Model with the methods you use
the most and then just extend all your models from the MY_Model.

To pass variables in the form you could use hidden input fields.

For HMVC you can get it from here:

Main Site Documentation

Download

You will need to check the forums for some update things to do with it.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#3
Thanks InsiteFX

I already use MY_Model the way you describe. Still my approach messes up my controller and views. It like 
I have to have double methods in one controller, like edit_article() and edit_store(). 

If I like to separate also them, my aproach is solving it.  But still I have to pass the returnpath back  and forth. Form opne accepts hiddn fields so Isend them there.

I think I will try my concept and then evaluate. Maybe I willl try HMVC but right now I don't have the CI experience to do it.

What do you think, would HMVC be better than switching to the other MVC, doing the work over there, and when that is finished redirect back using the returnpath?
Reply

#4
You could have one edit method and pass in a flag to tell it what to edit.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#5
(09-26-2017, 04:09 AM)InsiteFX Wrote: You could have one edit method and pass in a flag to tell it what to edit.

Hmm, that might be a good idea. I will try to stay in the articles controller and just pop over to Store_Model (where I have defined table Stores) to run the questions.
Reply

#6
Another approach is to use session data.

Before you go to the stores controller save the "returnURI".

PHP Code:
$_SESSION['returnURI'] = 'controller/method'

Then when stores is done

PHP Code:
redirect($this->session->returnURI); 

You would probably want to add some code to check the return of $this->session->returnURI before using it. You would also want to clear the session data at some point. Probably in the controller you are returning to.
Reply

#7
(09-26-2017, 11:20 AM)dave friend Wrote: Another approach is to use session data.

Before you go to the stores controller save the "returnURI".

PHP Code:
$_SESSION['returnURI'] = 'controller/method'

Then when stores is done

PHP Code:
redirect($this->session->returnURI); 

You would probably want to add some code to check the return of $this->session->returnURI before using it. You would also want to clear the session data at some point. Probably in the controller you are returning to.

Absolutely a nice approach, thanks!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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