Return to Snippet

Revision: 23818
at February 14, 2010 12:14 by jzumbrun


Initial Code
function SmartPageRoutes($action, $slug=NULL, $filename=NULL, $page=NULL)
		{
			if($slug!=NULL) $slug = $this->_generateSlug($slug);
			
			// Add link to routes file
			$routeFile = '../primary_app/config/routes.php';
		    $arr = file($routeFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
		    foreach ($arr as $key => $line)
		    {	
				if($action == 'add')
				{
					// Match all the public functions
			        if(preg_match('/DONOT ALTER THIS COMMENT... PLACE PAGE ROUTES BELOW/', $line))
						$arr[$key] = $line."n".'$route[''.$slug.''] = "'.$filename.'";'."n";
					if(preg_match('/DONOT ALTER THIS COMMENT... PLACE CLASS ROUTES BELOW/', $line))
						$arr[$key] = $line."n".'$route[''.$slug.'/(.*)'] = "'.$filename.'/$1";'."n";
				}
				else if($action == 'update')
				{
					// Match the previous route
					$urlPage = '$route[''.$page->linkSlug.'(.*)']';
					$urlClass = '$route[''.$page->linkSlug.'/(.*)(.*)']';
			        if(preg_match("~$urlPage~", $line, $child))
					{
						$arr[$key] = "n".'$route[''.$slug.$child[1].''] = "'.$filename.'";'."n";
					}
					if(preg_match("~$urlClass~", $line))
					{
						$arr[$key] = "n".'$route[''.$slug.$child[1].''] = "'.$filename.'/$1";'."n";
						$updateMatch = 1;
					}
					
				}
				else if($action == 'delete')
				{
					// Match the previous route
					$urlPage = '= "smart_pages/'.$page->pageSmartPageFileName;
					
			        if(preg_match("~$urlPage~", $line))
					{
						$arr[$key] = "";
					}
				}
		
		    }
				
			$routes = implode("n", $arr);
			
			// Let's make sure the file exists and is writable first.
			if (is_writable($routeFile))
			{
			    // In our example we're opening $filename in append mode.
			    // The file pointer is at the bottom of the file hence
			    // that's where $somecontent will go when we fwrite() it.
			    if (!$handle = fopen($routeFile, 'w')) 
				{
			         echo "Cannot open file ($routeFile)";
			         exit;
			    }
			
			    // Write $somecontent to our opened file.
			    if (fwrite($handle, $routes) === FALSE) 
				{
			        echo "Cannot write to file ($routeFile)";
			        exit;
			    }
			
			    fclose($handle);
				return true;
			} 
			else return false;
		}

Initial URL


Initial Description


Initial Title
CodeIgniter Route Insert

Initial Tags
textmate

Initial Language
PHP