Welcome Guest, Not a member yet? Register   Sign In
procedural to mvc
#1

[eluser]magiclko[/eluser]
Hi everybody,

I am confused a bit. Till now i used to write normal procedural php code, but now as i am switching to CI(MVC scenario) i am finding it a bit difficult to catch up.. Here is my problem.

suppose i currently have something like this in a file(main.php):

Code:
if ($whatyouwanttodo=="something1"
include("something1.php");
if ($whatyouwanttodo=="something2"
include("something2.php");
if ($whatyouwanttodo=="something3"
include("something3.php");
if ($whatyouwanttodo=="something4"
include("something4.php");
if ($whatyouwanttodo=="something5"
include("something5.php");
// like this so many files

where something{x}.php does some well defined work like it can create/edit users, it can login and it can do large number of core features too. So issue is how should i approach to implement same thing with MVC paradigm. Can it be done like this:
(main.php)
Code:
class Main extends Controller{

function index(){
if ($whatyouwanttodo=="something1"
{
include("something1.php");
// instantiate class and call its member functions appropriately
}
if ($whatyouwanttodo=="something2"
include("something2.php");
if ($whatyouwanttodo=="something3"
include("something3.php");
if ($whatyouwanttodo=="something4"
include("something4.php");
if ($whatyouwanttodo=="something5"
include("something5.php");
// like this so many files

}
}

and each something{x}.php will have a structure like this :
(something1.php)
Code:
class SOMETHING1 extends Controller
{
function index(){
//some code
}
function anotherFunction(){
//some code
}

// any member function above can load an appropriate view
}

I am writing SOMETHING{x} in uppercase deliberately because i don't want user to access this class directly or even via proper url like (example.com/index.php/something{x}/functionName). But thing is that i want the url to be site_url('main/index') all the way. so this approach fails... Any suggestions?
#2

[eluser]WanWizard[/eluser]
The idea behind MVC is that you split your functionality into logical and maintainable blocks, and turn them into controllers. You then map URI segments to these controllers.
So something like /admin/something1 will map to the admin controller, something1 method, /admin/something2 will map to the admin controller, something2 method, and so on.

Users DO access the controller directly, that's how MVC works. And I don't see why that would be a problem, the code executed is the same.

If you want all URI's to map to a single controller, you'll have to read up on HMVC. With that you can build a single front controller that receives all page requests, it then calls other controllers to fetch the html to build the page. This is something that is not built into CI, but there are several solutions available in the form of third party libraries that are heavily used.
#3

[eluser]magiclko[/eluser]
@WanWizard : I looked at HMVC, but i couldn't find proper documentation or a tutorial on how to use it Sad Just a small intro at https://bitbucket.org/wiredesignz/codeig.../wiki/Home
#4

[eluser]magiclko[/eluser]
Ok, so here is the deal:

I have read some tutorials, the intro page and i have a doubt... From what i've read i think its possible but i think i should confirm it too Smile

Suppose i call a module function from my main controller function and that module is either loading a view or returning some variables back to controller function and after i called that module function will it be possible that i can call another module's function which will be doing same thing and so on .. ??
#5

[eluser]WanWizard[/eluser]
Yes,

This is how I usually implement things. I have a single front controller, that uses the URI to get the name of a page template and all modules on that page from the database. It then calls all modules to retrieve the info, put that into an array, and load the template view to generate the page.
#6

[eluser]magiclko[/eluser]
so we can't load a complete view from one of our module's view and halt the execution there(i.e. not returning to main controler)?
#7

[eluser]magiclko[/eluser]
Ok.. i can't get it working. i am not getting any error though...I've transferred core files and third_party files to respective CI folders.. Any help?

I have main.php in application/controllers
Code:
<?php

class Main extends MX_Controller
{
    
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        modules::run('modules/login/index');
        $this->load->view("main_view");
    }
}
main_view.php in application/views
Code:
this is main view!

login.php in application/modules/login/controllers
Code:
<?php

class Login extends Controller {
    
    function index()
    {
        
        
        $this->load->view('login_view');
        

    }
}
login_view.php in application/modules/login/views
Code:
this is login view

I am always getting main_view content.. Any reason?
#8

[eluser]magiclko[/eluser]
i guess i need to set some setting or something?? or is it like i can't load a view from a module because whenever i try to do it, i don't see anything Sad

P.S. workaround - i returned a view from module controller method , but just a question when i say $this->load->view(); it seems that it looks in application/views folder only.. how to add application/modules/ folder to it?
#9

[eluser]WanWizard[/eluser]
Code:
modules::run('modules/login/index');
doesn't output anything, it returns results.

The idea is that you'll use
Code:
// fetch the login widget
$widgets['login'] = modules::run('modules/login/index');

// parse and display the page template
$this->load->view('page_template', $widgets);

// ... and then in your template view use 'echo $login' to display the widget
#10

[eluser]magiclko[/eluser]
i see. so in my login widget if i have to return a partial view then how to do so?

return $this->load->view('myview');

But it looks in application/views folder only for 'myview.php' and as it is not there(because 'myview.php' is a login module view(i.e. location - application/modules/login/views/myview.php)) it is showing me an error. that myview.php can't be found. Am i doing something wrong?




Theme © iAndrew 2016 - Forum software by © MyBB