0
votes

Not a back-end guy by any means, but can navigate my way through the basics. My question is this, I have the below function for a benefits page I'm doing. Wanted to simplify the sub-pages versus creating multiple controller functions and route redirects. So I decided to go the URI as a variable route...not sure if this is clean way of doing it or not (usually I only go this route when dynamically creating pages via db). But it works.

If I go to website.com/benefits/{section} the IFs work as they should. However, if I go to website.com/benefits/ I get a 404 page - obviously. I have nothing to handle that request - only because I don't know how. How do I redirect to the home page is a user visits website.com/benefits/ and no uri (i.e. $section) was defined?

EDIT: Or would it be wise to just create a separate function benefits(){} function along with the one below? Is this good practice?

Controller function:

function benefits($section)
{
    if($section == 'fitness')
    {
        echo('fitness page!');
    }
    elseif($section == 'health')
    {
        echo('health page!');
    }
    elseif($section == 'beauty')
    {
        echo('beauty page!');
    }
    elseif($section == 'sports')
    {
        echo('sports page!');
    }
    else
    {
        redirect('/');
    }
}
3
What is "the home page" you want to redirect? Did you mean redirect('/'); is "redirect to the home page"? - anvoz

3 Answers

1
votes

Your redirect seems to be giving 404. If you want to access /benefits/, just make a default case (in your case, the else part).

But see what I did - I've set up a default value for $section to index, so that would denote your index page, making all unspecified value do the redirect:

function benefits($section = 'index')
{
    if($section == 'fitness')
    {
        echo('fitness page!');
    }
    elseif($section == 'health')
    {
        echo('health page!');
    }
    elseif($section == 'beauty')
    {
        echo('beauty page!');
    }
    elseif($section == 'sports')
    {
        echo('sports page!');
    }
    elseif($section == 'index')
    {
        echo('benefits index page!');
    }
    else
    {
        redirect('/');
    }
}

But, since the question is not very clear, there could be another solution.

Make sure your controller is called 'Benefits', and rename your function to 'index'. This would work for URLs like site.com/benefits.

See more info about using controllers here.

0
votes

You may try this (add default function parameter value) to redirect to the home page when you visit website.com/benefits/: function benefits($section = FALSE) {}

I was missing the fact that you have to use routing rule for this. In sum up, you want the route to handle 2 scenarios:

1.Go to website.com/benefits/{section}: echo something
2.Otherwise, go to website.com/benefits/ or the section was not defined in IFs statements: redirect to the home page (website.com/)

In fact, website.com/benefits/ is not a separated case, it should belong to the else case literally. You just want to handle the else case correctly but got a bug when you access website.com/benefits/. You don't want to handle the index section if you don't need to. So you probably use a wrong routing rule.

I tried to reproduce your problem and if you used the setting in the guide of CodeIgniter like $route['benefits/(:any)'] = 'content/benefits/$1'; you will get a 404 page as you mentioned.

Here is the correct routing rule for your problem, without changing any on your benefits function:

$route['benefits/?(.*)'] = 'content/benefits/$1';
-1
votes

That's the index function you are talking about. :D, and the function index() is the default function that gets executed if there is no action defined in the url. No offence, but still not sure what you are doing here.