CodeIgniter Forums
I'm having trouble following the MVC standard. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Regional User Groups (https://forum.codeigniter.com/forumdisplay.php?fid=25)
+--- Thread: I'm having trouble following the MVC standard. (/showthread.php?tid=87038)



I'm having trouble following the MVC standard. - LieoUhlerredyh - 03-09-2023

I'm new to CodeIgniter and I may have made a silly mistake. I have a view called "menu.php" with several options, such as "Query" and "Registration". When I click on "Query", it should call the view "queryProducts.php", but it's not working. How can I call it from the menu? It worked fine without the framework, but I'm having trouble following the MVC standard.


RE: I'm having trouble following the MVC standard. - qury - 03-09-2023

(03-09-2023, 02:47 AM)LieoUhlerredyh Wrote: I'm new to CodeIgniter and I may have made a silly mistake. I have a view called "menu.php" with several options, such as "Query" and "Registration". When I click on "Query", it should call the view "queryProducts.php", but it's not working. How can I call it from the menu? It worked fine without the framework, but I'm having trouble following the MVC standard.

Hi

You can do if you following. 

In you controller you should have different methods for each view:

PHP Code:
// Assume this is your default view¬† and the route is /home
public function index(){
    return  
view('header')
    .
view('menu')
    .
view('somepage')
    .
view('footer');
}

// this would be your path /home/query
public function query(){
    return  
view('header')
    .
view('menu')
    .
view('queryProducts')
    .
view('footer');
}

// this would be /home/register
public funtion register(){
    return  
view('header')
    .
view('menu')
    .
view('register')
    .
view('footer');


I have assumed you would have the following view files in your Views folder:
- header.php
- footer.php
- menu.php
- queryProducts.php
- register.php
- somepage.php

I assume you can create links in your view file using site_url() as described here: https://codeigniter4.github.io/CodeIgniter4/helpers/url_helper.html

Also please read: https://codeigniter4.github.io/CodeIgniter4/tutorial/static_pages.html


RE: I'm having trouble following the MVC standard. - captain-sensible - 03-09-2023

i do mostly everthing from routes .So suppose on dev localhost , I have a view displaying : http://127.0.0.7/blogArticle/Southhall-Church-Park

via

Code:
<a href = blogArticle/Southhall-Church-Park> Southhall-Church-Park         </a>

blogArticle is connected to a route and Southhall-Church-Park is a slug I have in a slug field of slug

in app/config/Routes.php i have :

Code:
$routes->get('blogArticle/(:segment)', 'Blog::showArticle/$1');


so in the above anything in a url with blogArticle is going to get picked up

whats on the left i.e the url section is going to get passed to a controller called Blog.php and processes in a function as follows
Code:
Blog::showArticle/$1


Code:
public function showArticle($theSlug)
                    
                    {
                                         
                    $handle = new BlogModel();
                    $result= $handle->getArticle($theSlug);
                        
                    $data =[
                    'title'=>$theSlug,
                    'blog'=>$result,
                    'date'=>$this->myDate
                    ];
                            
                        
                    echo view('blogArticle',$data);
                    
                        
                    
                    }

So you can design how menu or a href links in views are handled