Welcome Guest, Not a member yet? Register   Sign In
How to load view if function not exists?
#1

[eluser]codeworxx[/eluser]
Hey Guys,

me again Smile I have another Problem with a Controller:

I have an URL like this index.php/category/systems

And a simple Controller:
Code:
class Listings extends Controller
{

    function Listings()
    {
        parent::Controller();
                $data['segment'] = $this->uri->segment( 2 );
                $this->load->view( 'category', $data );
    }
    
}

Now the View is not loaded (Error 404), because CodeIgniter is looking for the function 'systems', but "Systems" is the Category which i am looking for in the Database to create the View. So where's my Mistake?

Thanks again,

Sascha
#2

[eluser]pistolPete[/eluser]
You could use the _remap() function: http://ellislab.com/codeigniter/user-gui...#remapping

But you still need one functione besides the constructor where you remap the requests to.
#3

[eluser]BrianDHall[/eluser]
I think you could also use routes to reroute everything to listings/somefunction, but again you still need another function besides the controller.
#4

[eluser]jedd[/eluser]
[quote author="codeworxx" date="1257215469"]Hey Guys,
Now the View is not loaded (Error 404), because CodeIgniter is looking for the function 'systems', but "Systems" is the Category which i am looking for in the Database to create the View. So where's my Mistake?
[/quote]

My opinion (!) is that you should change your controller and method name to reflect the URL you are happy with. While I acknowledge that you can route around all kinds of mess, and no offence is intended here -- if you have to ask how to do this, then for the moment you should probably be adopting the simplest approach available.

So, rather than trying to use this url: index.php/category/systems to get to this method: Controller / Listings ... I'd suggest you do the following:

Try to think of your controller names as nouns, and your method names as verbs. That is, the controller handles a resource - a thing if you prefer - and your methods handle stuff you can do with those resources. Hence you might have a Listing controller with methods like 'show', 'edit', 'delete'.

Don't put much in your constructor at all at this stage (as you've got here) as it will just confuse you. Similar you probably want to leave the index() method almost empty - perhaps a simple _remap() or redirect() to your default / preferred method.

Don't use segment() calls unless you really need to - instead, for this kind of thing, you use the parameter feature for controller functions - a really feeble example follows:

Code:
// Somewhere in your Category controller

function  systems  ( $thing )  {
    // do some sanity tests on the content of $thing, and if okay ...
    $data['sane_thing']  = $thing;
    $this->load->view( 'category', $data );
    }
#5

[eluser]codeworxx[/eluser]
Thanks all for your replys. I will read the #remap Infos.

@jedd:
The "Problem" is, that i create a Webshop System. The URLs are designed as follows:

Categories e.g.:
category/systems (pc systems)
category/memory (ram, hd, ....)
and so on

then i have
Listings e.g.:
listings/systems/hewlettpackard
listings/systems/acer

and so on...

The Categories and Listings are created dynamically.

Also in Future

search/my search phrase

....
....

Greetings,
Sascha
#6

[eluser]John_Betong[/eluser]
[quote author="codeworxx" date="1257360577"]Thanks all for your replys. I will read the #remap Infos.

@jedd:
The "Problem" is, that i create a Webshop System. The URLs are designed as follows:

Categories e.g.:
category/systems (pc systems)
category/memory (ram, hd, ....)
and so on

then i have
Listings e.g.:
listings/systems/hewlettpackard
listings/systems/acer

and so on...

The Categories and Listings are created dynamically.

Also in Future

search/my search phrase

....
....

Greetings,
Sascha[/quote]
 
I have a similar problem and have found one way that works for me Smile

Try this:
Code:
// ./config/routes.php

  $routes['category/systems/:any'] = 'category/systems';
  $routes['category/memory/:any']   = 'category/memory';
  $routes['listing/systems/:any']   = 'listings';



// ./controllers/category.php

function systems()
{
  $type = $this->uri->segment(2);
  $data['type'] = $this->model_type->get_relevant($type); // maybe return NULL
  ...
  ...
  $page = $this->load->view('the_types', $data);
}
 
 
 





}
#7

[eluser]jedd[/eluser]
Quote:The "Problem" is, that i create a Webshop System. The URLs are designed as follows:

Do you mean you have created, or you are about to create?

Do you mean that the URLs are designed already, or that this is what you think you need to do in the application you haven't written yet?

'Designed' may be too strong a word, I think, here.

I think if you just bring everything down a level, and have your dynamically created bits as the parameter to the method(s) in your controllers, your life will be somewhat easier.
#8

[eluser]codeworxx[/eluser]
@John_Betong

It's a nice start, but the categories may created, changed, added by the owner of the Shop, so i cannot define anything in the routes.php!!

@Jedd

I'm about to create - but the URL "Design" is ready and chosen by the Customer. With "Designed" I just mean how URLs have to look like.

What do you mean with "bring everything down a level"??? Don't understand that?!?!
#9

[eluser]jedd[/eluser]
[quote author="codeworxx" date="1257372504"]
What do you mean with "bring everything down a level"??? Don't understand that?!?!
[/quote]

Have your dynamically created bits as the parameter to the method(s) in your controllers.

Consequently have methods like Categories() and Listings() - though as I said before, I think having nouns as methods is ultimately confusing.
#10

[eluser]codeworxx[/eluser]
You mean e.g.

Code:
shop/categories/...
shop/listings/...

??? Where "shop" is the Controller and "categories" and "listings" is the method?




Theme © iAndrew 2016 - Forum software by © MyBB