• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic Routes from an Library (?)

#1
[eluser]@MaxG[/eluser]
Hey Guys,

I have created an library to route things from an database dynamically.
Here is the Code(application/libraries/route.php):
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Route{
public $_ci;

public function __construct()
{
$this->_ci =& get_instance();
}
public function GetAllRoutes()
{
$q = $this->_ci->db->get('routes');
return $q->result_array();
}
public function AddNewRoute($RouteId, $RouteStart, $RouteAim)
{
$ToInsert = array(
'route_id' => $RouteId,
'route_start' => $RouteStart,
'route_aim' => $RouteAim
);
If($this->_ci->db->insert('routes', $ToInsert))
{
return true;
}
return false;
}
}

Then in the file(application/config/routes.php):
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


$route['default_controller'] = "page";
$route['404_override'] = '';
Foreach($this->route->GetAllRoutes as $GetAllRoutes){
$route[$GetAllRoutes['route_start']] = $GetAllRoutes['route_aim'];
}


/* End of file routes.php */
/* Location: ./application/config/routes.php */

I have autoloaded the library.

The folowing Error comes:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: MY_Router::$route

Filename: config/routes.php

Line Number: 43

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: config/routes.php

Line Number: 43

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: config/routes.php

Line Number: 43

Oh.. Iam using HMVC(MX).


Would be SO nice, if everyone could help me.

Sorry for my bad bad english, iam german and to stupid to learn it :-D


Greets

#2
[eluser]PhilTem[/eluser]
You can't use dynamic routing in that way since the routes.php file will be loaded way earlier than any other things. Basically it is run and validated after the CI_Config and CI_Loader class is instantiated.
Therefore you can't use your library. Furthermore you're not in an object-scope hence $this is not available.

What you want to do is extend CI_Router with MY_Router, load the database and query your routes from the database. That's the only way I can think of real dynamic routing.

#3
[eluser]@MaxG[/eluser]
Heyo,

How can I do this? HMVC uses MY_Router already.

Greets.

#4
[eluser]PhilTem[/eluser]
HMVC doesn't really supply a functional MY_Router, it does however supply a file called MY_Router to load the MX_Router rather than the CI_Router.

You can modify MY_Router in APPPATH . 'core/' to match your needs and to do the library loading (use load_class rather than $this->load->library()) You can find the syntax of load_class in BASEPATH . 'core/Common.php' so you know what it does.

Your case is a little harder than getting dynamic routes for the modules but it is feasible just requires a lot of understanding of CI business logic and how all the gears work together Wink


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.