PHP CodeIgniter路由插入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP CodeIgniter路由插入相关的知识,希望对你有一定的参考价值。

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;
		}

以上是关于PHP CodeIgniter路由插入的主要内容,如果未能解决你的问题,请参考以下文章

带有 Wiredesignz HMVC 路由的 CodeIgniter

codeigniter 路由问题

无法使用 php codeigniter 插入批量记录

使用 CodeIgniter 在子文件夹中路由控制器

CodeIgniter 路由不起作用

如何在 codeigniter(或 php)中获取插入的新 id? [复制]