Welcome Guest, Not a member yet? Register   Sign In
Getting errors after changing routes
#1

[eluser]jshultz[/eluser]
I was working on making my routes file dynamically updated from the database. Unfortunately, my cunning plan has caused some problems.

I added this line to the end of routes.php in the config file:

Code:
include_once APPPATH . "cache/routes.php";

The routes.php file in the cache folder is getting updated automatically which is nice. Currently, it looks likes this:

Code:
$route["index"] = "pages/index/1";
$route["About Us/subpagetest"] = "pages/index/4";
$route["About Us"] = "pages/index/3";

The problem is, i'm getting these errors at the top of every page:



Quote:A PHP Error was encountered
Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/jshultz/public_html/application/cache/routes.php:3)

Filename: core/Security.php

Line Number: 188



A PHP Error was encountered
Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/jshultz/public_html/application/cache/routes.php:3)

Filename: libraries/Session.php

Line Number: 672


I thought maybe it was a permissions error but I don't know? Any ideas?
#2

[eluser]jshultz[/eluser]
Here's the solution. First, routes.php in the application/cache folder needs to have <?php at the beginning. Since the controller that writes the file writes a whole new file each time I needed to update that controller to the following:

Code:
public function save_routes()
  {

   // this simply returns all the pages from my database
   $routes = $this->Pages_model->get_all($this->siteid);

   // write out the PHP array to the file with help from the file helper
   if ( !empty( $routes ) )
   {
    // for every page in the database, get the route using the recursive function - _get_route()
    foreach( $routes->result_array() as $route )
    {
     $data[] = '$route["' . $this->_get_route($route['pageid']) . '"] = "' . "pages/index/{$route['pageid']}" . '";';
    }

    $output = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n";

    $output .= implode("\n", $data);

    $this->load->helper('file');
    write_file(APPPATH . "cache/routes.php", $output);
   }




Theme © iAndrew 2016 - Forum software by © MyBB