Welcome Guest, Not a member yet? Register   Sign In
Two conceptual questions (about models and controllers)
#1

[eluser]Marcus Cavalcanti[/eluser]
CI People,

I have two questions and i think they can help me.

First question:

I have a site with frontend (users view) and backend (admin view), probably i need to use the models in this two cases (backend and frontend). My question is: How to use the same models in this two cases?

Second question:

In my fronted, my site have’s a header, menu, content and footer, it is a classic model site, and the unique container has change is the content. Mu question: The best way to make this is create a “My_Controller” that extends the CI Controller, and the pages Controlllers (blog, forum, contact) extends “My_Controller”?

Thanks!

ps: sorry for my poor english Smile
#2

[eluser]marcoss[/eluser]
[quote author="Marcus Cavalcanti" date="1183518998"]
First question:

I have a site with frontend (users view) and backend (admin view), probably i need to use the models in this two cases (backend and frontend). My question is: How to use the same models in this two cases?
[/quote]

You can load the same model in every controller if you have too Wink

[quote author="Marcus Cavalcanti" date="1183518998"]
Second question:

In my fronted, my site have’s a header, menu, content and footer, it is a classic model site, and the unique container has change is the content. Mu question: The best way to make this is create a “My_Controller” that extends the CI Controller, and the pages Controlllers (blog, forum, contact) extends “My_Controller”?
[/quote]

No, just create one controller called 'pages' and then every method inside that controller will be your actual page. (www.site.com/pages/method)
#3

[eluser]Marcus Cavalcanti[/eluser]
Quote:You can load the same model in every controller if you have too Wink

But i organized this in two different applications, in different folders.

example:

Code:
/backend
    -controllers
    -views
/frontend
    - controllers
    -views
/models (for all applications in this "CI installation")


Quote:No, just create one controller called 'pages' and then every method inside that controller will be your actual page. (www.site.com/pages/method)

Maybe it's not right, because i have one controller for each page.

example:

www.site.com/blog/display_entries
www.site.com/contact/address
www.site.com/team/members
#4

[eluser]Rick Jolly[/eluser]
[quote author="marcoss" date="1183677805"]
No, just create one controller called 'pages' and then every method inside that controller will be your actual page. (www.site.com/pages/method)[/quote]

Are you suggesting one controller for an entire site?

Typically, you'd have one controller per page. Partial views (header, footer, etc.) can be loaded in one place - your own library called from a controller's constructor, or a parent controller. So yes, Marcus, you could extend the CI Controller as a library (My_Controller) and load the partial views from the My_Controller's constructor. I prefer to create my own parent controllers and include them because it is more flexible. I can have any number of parents. For example, I can have a site-wide custom parent controller (that extends the CI Controller) and an admin parent controller that extends that. I do my security checks and backend partial view loading from the base admin controller constructor. So for any admin controller I just include my admin parent controller, extend it, and call its constructor.
Code:
// example of extending a custom parent controller

include(APPPATH . '/controllers/base_admin_controller.php');

class SomeAdminController extends BaseAdminController
{
  function SomeAdminController ()
  {
    parent::BaseAdminController();
  }
}
#5

[eluser]Newton Wagner[/eluser]
Hi Marcus,

I think you are brazilian, but lets keep it in english! Smile. If want to contact me in portuguese, use my blog (link on signature) contact or the private message.

1.

You can use modular folders for your controller. Here we have this folder structure:

Code:
/controllers
  - /module1
    - Controller.php
  - /module2
    - ControllerB.php
    - ControllerC.php
/models
  - Model.php
/views
  - /module1
    - view.php
  - /module2
    - viewB.php
    - viewC.php
  - template.php

You can name Modules as 'Frontend' and 'Backend' if you want to.


2.

User guide said to NOT extend the Controller class using MY_Controller. I think it is because if you need more then one extension, you will be in troubles.

Create a Controller inside your Application/Controller folder with this functionalities, and extend your controllers of it.

I made a method called renderView() in the Parent Controller, and it loads the Header, Footer, Menus and another things I need.
#6

[eluser]marcoss[/eluser]
[quote author="Rick Jolly" date="1183679573"][quote author="marcoss" date="1183677805"]
No, just create one controller called 'pages' and then every method inside that controller will be your actual page. (www.site.com/pages/method)[/quote]

Are you suggesting one controller for an entire site?
[/quote]

He said,

Quote:In my fronted, my site have’s a header, menu, content and footer, it is a classic model site, and the unique container has change is the content.

So I've asummed his site strucutre was rather simple, pages/home, pages/about, pages/contact, etc.
#7

[eluser]OwanH[/eluser]
[quote author="Marcus Cavalcanti" date="1183678497"]
Quote:You can load the same model in every controller if you have too Wink

But i organized this in two different applications, in different folders.

example:

Code:
/backend
    -controllers
    -views
/frontend
    - controllers
    -views
/models (for all applications in this "CI installation")


Quote:No, just create one controller called 'pages' and then every method inside that controller will be your actual page. (www.site.com/pages/method)

Maybe it's not right, because i have one controller for each page.

example:

www.site.com/blog/display_entries
www.site.com/contact/address
www.site.com/team/members[/quote]

Hi Marcus, maybe you didn't understand what Marcoss meant. Maybe I can help clarify Smile.

1. Models
From the CI Docs:

Quote:Models are PHP classes that are designed to work with information in your database.

So think of a model as an abstraction layer that lets you interface into methods that can allow you to retrieve, insert, update and delete information you have stored in your database. That's it, plain and simple. Your controller loads the model and decides how it will use and manipulate that information then passes it on to your view where it is formatted and presented. Models do not need to mimic your controller directory structure.

For example, say you are implementing a photo gallery and you implement backend controllers and views for creating, editing and deleting photo galleries and uploading photos to the galleries. You also want to implement frontend controllers and views for allowing those photo galleries and photos to be viewed publicly. Although you are implementing separate controllers you can have one (1) single Model class that allows you to retrieve and update information on your photo galleries and the photos you upload and add. Why? Because you are dealing with the same data in both the frontend and backend, you're just accessing and manipulating it for different purposes in each case.

2. Controllers
When Marcoss said just create one controller and then every method inside that controller will be your actual page here's an example of what he means that should help put it into context. Say you are using CI to power a Blog. Consider the code below, which shows a controller class named Blog and assume it resides in the root of your application/controllers folder (we're ignoring any .htaccess rewrite rules, etc.):

Code:
class Blog extends Controller {

    function index()
    {
        [some PHP code goes here];
    }

    function entry($entry_id)
    {
        [some PHP code goes here];
    }

    function category($category_id)
    {
        [some PHP code goes here];
    }
}

The above controller has 3 methods, each of which can be used to represent an actual "template" page in your Blog. How? Your index() method can load your view for the blog's home page, which could show a list of the most recent say 10 entries for example, category links, etc. etc. CI would invoke this method if either of the following pages are requested:

Code:
http://www.site.com/blog/
Code:
http://www.site.com/blog/index/

The entry() method can load a view that shows a single published blog entry, and could be invoked by a page request such as:

Code:
http://www.site.com/blog/entry/24/

The category() method can load a view that displays an archive of entries all of which are published in a particular blog category, and could be invoked by a page request such as:

Code:
http://www.site.com/blog/category/8/

In this example, we have a number of "pages" or "page views", all served up by a single controller. In the example pages that you listed above however;

www.site.com/blog/display_entries
www.site.com/contact/address
www.site.com/team/members

you wouldn't be able to use a single controller to satisfy those requests (unless you set up rewrite rules) because essentially the segment structure does not allow it. For instance with the page

www.site.com/blog/display_entries

you are telling CI that there is either a controller named Blog with a method called display_entries that should be invoked or you have a controller named Display_entries that is located in a sub-folder named blog located within your application's controllers folder. Following that pattern you should be able to see how the other two pages can't be handled by the same controller.


Well Marcus, hope I've helped. Sorry if I wrote too much Smile.
#8

[eluser]Marcus Cavalcanti[/eluser]
Quote:Are you suggesting one controller for an entire site?

No, no .. i have many controllers, one controlle for each page. I have two applications in my site (fronted and backend) and i need share my models to this two applications.

Newton .. thanks for your attention, you understood what i want.

Marcoss .. no, its more complicated of this .. i need one controller for each page, because each controller have's many methods.
#9

[eluser]marcoss[/eluser]
OwanH made a good breakdown of my initial suggestion, you should be able to work from that.

And again, I'm not suggesting a single controller to serve the whole site, this is what i mean.

Code:
/application
    /controllers
        /backend
            -controller
            -controller
        /frontend
            -controller
            -controller
    /models
        -model
        -model
    /views
        /backend
            -view
            -view
        /frontend
            -view
            -view
#10

[eluser]Marcus Cavalcanti[/eluser]
[quote author="OwanH" date="1183683068"]You can load the same model in every controller if you have too Wink

But i organized this in two different applications, in different folders.

Well Marcus, hope I've helped. Sorry if I wrote too much Smile.[/quote]

Thanks OwanH, you he was almost perfect Smile

I know how controllers/models/views works, i understand MVC, but my doubts is in relation about CI architecture.

The doubt about models is solved hehe. So, i can use models in external directory of applications and share this model with all applications, right? Ok!

The question about "change content" is not very clear hehe in my site i have each controller for each page, like a: Blog.php, Contact.php, Forums.php ... and each controller have's a specify methods, so in my page container (header, menu, content, footer) the only part has change is the "content", in the content i have a specify content for each page. This is confused, i will try to use examples.

I have the url: www.site.com/blog/entries and www.site.com/contact/address

The structure of this page are the same. Same header, same menu, same footer, what it change is the content and the content are processed for specific controller, in this case Blog.php (entries() function) and Contact.php (address() function). So i "My Controller" i would have functions for header, footer, menu, contants, session and what be common to all pages and in my controllers i extend this control (My Controller)? Is the best practice to make this?

PS: sorry for my poor english and the confusion of my post Smile




Theme © iAndrew 2016 - Forum software by © MyBB