Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Navigation
#1

[eluser]elmne[/eluser]
I would like to understand how navigation works with codeigniter

It seems naviagation can be laid out as
Code:
controller

    function(s)

           parameter(s)

Whereby the controller is pointed to, with the function to be used indicated and optionally parameters passed to the function.


An example would be if the application handled news

Code:
News controller            

             add (news)
          
             edit (news)

             delete (news)

             View (news)

                               news_id=7

The result would be a path like this in the browser address bar

Code:
index.php/news/view/id=7

Whereby a request is being made to the news controller to run the view function and pass the parameter 7

Or similarly when wanting to view a customer

Code:
controller

          customer
                
                    add customer

                    edit customer

                    view customer

                              customer_id=3


.index.php/customer/view/id=3

If the above is right, then how does one achieve such links

Code:
index.php/admin/customer


index.php/health/news/articlename

So that the categories of say the news item are shown, because when the controller returns the article to the browser, it would be desirable to show the category of the item returned as shown above, the item is in
-the health category
-under news
-and the article name is shown

But codeigniter seem to have the urls reserved to point to controllers, functions, pass parameters and optionally have physical paths to folders preceding the controller.
However this does not apply to the structure of the content in the database.

Code:
index.php/folder option/controller/function/parameter

What if the information is in a database and the url should reflect categories of an item stored in a database, how would this work?

Is there a way to achieve a url

Code:
index.php/category/sub-category/item

based on database values?
#2

[eluser]Narkboy[/eluser]
OK, thats one big question!

So - as you're correctly said, CI accepts segments that point to controller, then function, then pass parameters.

Thus:

example.com/foo/bar/bob/betty

Would ask CI to call the foo controller, find and run the bar function within it, and pass to that function two vars - 'bob' and 'betty'.

Now, importantly, you define exactly what you want that function to achieve. Literally anything can be done at the point.

Here's where you seem to be getting confused. Lets say that you want the link:
Code:
example.com/health/news/article_name
to load the article specified. You're asking why CI wants that to point to a function.

Simply - because that's what CI does. So how do we get that to work for you?

Well, the easiest way is to make a Health controller, with a news function in it. That function receives the article_name as it's only parameter, fetches it from the database, and loads a view containing that article. Like so:

Code:
class Health extends Controller {

    function news( $title ) {
        if ( $title ) {
            // Get the article from the db

            // Load a view and pass the article content to it.
    }
}

Great! But, you say, 'health' is one of many categories, so I have to write a controller for every one?

Nope. If you have a series of articles, within a variety of topics (categories), then what you want to do is create a controller than handles them all:

Code:
class View extends Controller {

    function _remap ( $cat , $topic , $title ) {
        // Grab and display the article.
    }

}

_remap is a special function name for controllers - if _remap is within a controller, everything call its:

Code:
example.com/view/health/news/article_title
example.com/view/sport/interview/tiger_woods
example.com/view/kids/tv/sponge_bob
example.com/view/tech/reviews/ipodv50

All these URLs will call that _remap function - the following segments will act as variables, which you can use to query a db and get relevant articles.

Make sense?


/B
#3

[eluser]elmne[/eluser]
Yes thanks for that, it makes sense.

So how are the links set in the data returned after the database is queried, since the url will need to consist of variables if items are to be linked to.

Would i use a uri helper, like the example shown in the documentation

Code:
echo site_url("news/local/123");

Code:
$segments = array('news', 'local', '123');

echo site_url($segments);


Also, does the _remap function work like a url handler?
#4

[eluser]InsiteFX[/eluser]
Read the CodeIgniter User Guide.

CodeIgniter User Guide - URI Class

InsiteFX
#5

[eluser]elmne[/eluser]
InsiteFX, your response was not helpful. The first responder provided helpful information and i was seeking clarity in line with that.

It doesn't help pointing me back to documentation i already read.

I have been on that link and that's why i took quotes from it. Your answer doesn't help me as to whether _remap works as a url handler and whether using the uri helper to construct a url will get variables from the database to form a url that will work.
#6

[eluser]Narkboy[/eluser]
Don't mind InsiteFX - he's got low tolerance but he knows his stuff, and it's been a while since he was new to CI!

So - creating links is a bit of a thing, so I'll deal with _remap first.

No, _remap is not a URI handler, and TBH I'm not sure where it is in the CI core. Basically, if you have a function named _remap within any controller class, that's the only method that will be called.

Links, then. If your 'base_url' (in config.php) is 'example.com' and your 'index_page' is 'index.php':
Code:
echo site_url("news/local/123");
will produce:
Code:
http://example.com/index.php/news/local/123

Nothing more, nothing less. The 'site_url' function works to generate links for you, so if you change your base_url or index_page, there's no re-coding to be done. It saves me about a billion hours when moving from local dev settings to staging to live.

As long as you've setup (in the above example) the 'news' controller to accept and handle 'local' and '123' - either as function / param or 2 params for the _remap function, then that URI will load fine.

Getting clearer now?

/B

EDIT:

Just noticed your bit about how you get stuff from the db into links.

First, create a model with a function that queries your database and returns a list of article titles, together with their category and sub-category. In your controller, load the model and call that function, catching the returned data in an array. Pass that array to a view. In the view, you loop the array, and echo each one, using 'site_url' as a link.

It takes a while to appreciate exactly where and why different parts of the code are placed, and CI is pretty forgiving so you can be very abusive and put things in the wrong place without it breaking.

Views contain mark-up; this is what is rendered. Typically light on PHP (except for if and loops) and heavy of HTML / JavaScript.

Models handle data input / output and manipulation. Database calls, API calls - if it involves getting info from somewhere, stick it in a model.

Controllers are the glue - they are called by the user grabbing a given URL, and they contain the logic that dictates what model functions are called, how the data is handled and which views to show. They also do the work of form processing (though I do this in models and use the controllers to pass data around) and other tasks.

CI also has helpers, which are simple stand-alone functions and libraries which can be used for all sorts, but I tend to use when I need a more complex and less-CI / more-OOP approach to something.

Smile
#7

[eluser]elmne[/eluser]
Thanks,

Your answer was helpful. Atleast now i know that using _remap will overide any other function in a controller and that it will process items as parameters unlike a controller without _remap that reads function/parameter.

And also the insight of how to generate urls based on database items.
#8

[eluser]InsiteFX[/eluser]
_remap is a method in your code that you create yourself.

The CodeIgniter core file system/codeigniter/codeigniter.php
checks to see if you have created a _remap method in your code!

For more information, view the codeigniter.php file.

Another good file to look at is CodeIgniter's loader.php

InsiteFX
#9

[eluser]elmne[/eluser]
Thanks for the addon




Theme © iAndrew 2016 - Forum software by © MyBB