Can someone tell me, where the issue is ??
This is my controller
class Support extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('support_model');
$urlarray = array("index","delete");
if(!in_array($this->uri->segment(2),$urlarray)){
$this->viewticket($this->uri->segment(2));
}
}
public function viewticket($id){
if(!empty($id)){
$this->load->view('templates/logged_header');
$this->load->view('support/view');
$this->load->view('templates/footer');
}
}
}
Here is my routes.php
$route['default_controller'] = "welcome";
$route['benefits'] = 'welcome/benefits';
$route['faqs'] = 'welcome/faqs';
$route['distributors'] = 'welcome/distributors';
$route['contact'] = 'welcome/contact';
$route['purchase'] = 'welcome/purchase';
//login routes
$route['login'] = 'login/index';
$route['logout'] = 'login/logout';
$route['404_override'] = '';
localhost/ciproj/support/hello-world gives me 404 Page Not Found error
If I use exit; after $this->load->view('templates/footer');, the page is showing me blank page.
I don't have anything in routes related to support and every other method is working
Is there anything that i'm missing in routes ??
Thanks for the help.
Judging the title, first of all check if your server is running PHP using CGI/FastCGI or not (you could simply check that by phpinfo()).
If so, change the following in config.php:
$config['uri_protocol'] = "REQUEST_URI";
Back to the topic, you could achieve that by using the single-line route below within your routes.php file:
$route['support/(?!index)(?!delete)(:any)'] = "support/viewticket/$1";
And remove these lines from your __construct method:
$urlarray = array("index","delete");
if(!in_array($this->uri->segment(2),$urlarray)){
$this->viewticket($this->uri->segment(2));
}
Let me know how it works.
Since the above answer did not work for me, I just added this function in my default_controller, and it worked.
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
Related
I started working on a platform on CodeIgniter thanks to my work. The platform was started since before, so i just took the project. The thing is that i hadn't work with CI, so i just had a fast tutorial, and started developing based on how the platform was built. Now, to start, i decided to make a new page to to put a list of objects and understand a little how the PHP communicated with the HTML. The problem is that, when i go to the URL that i defined on routes.php, it gives an error "Requested resource does not exist", and i don't know what i'm doing wrong, while something similar works on other modules of the platform.
The files i'm using are:
list.php
// stuff
<?php if (has_access('agroindustrias')): ?>
<li class="<?php get_li_class('agroindustrias', $active); ?>" >
Agroindustrias
</li>
<?php endif ?>
// stuff
agroindustria.php
<?php
if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Agroindustria extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('agroindustria_model', 'agroindustria');
}
public function index($offset = 0)
{
$data['links'] = $this->paginate($this->agroindustria, 'agroindustrias', $offset);
$data['permisos'] = $this->getPermissions('usuario');
$data['active'] = 'usuarios';
$data['agroindustrias'] = $this->agroindustria->limit($this->limit, $offset)->get_all();
$data['submenu'] = $this->load->view('administracion/menu', $data, true);
$this->template->write_view('content', 'administracion/agroindustrias/list', $data);
$this->template->render();
}
}
routes.php(Updated with all the file, only the one before "reportes" doesn't work)
$route['administracion/agroindustria'] = 'agroindustria/index';
My URL is: http://localhost/work/administracion/agroindustria
The other controllers works with something similar, reason that i don't know what i'm doing wrong, if i still need to add something to another file, or if something that i wrote is wrong. Thanks in advance.
After the things that #Antony told me to check, i was able to find the error, it wasn't actually an error, just that the people before worked very much in the platform, that i did not undertand the mothodology to add new modules to the platform. All that i needed to do was register the new module, and the platform added the new URL as i wanted. Thanks Antony for your help!
1st of all rename agroindustria.php to Agroindustria.php if 1st letter not in upper case
if you are using method like this
public function index($offset = 0){
....
....
....
}
then you need to change routes like
$route['administracion/agroindustria/(:num)'] = 'agroindustria/index/$1';
and url
http://localhost/work/administracion/agroindustria/0
The best solution is change in your controller
//if offset not change
public function index(){
$offset = 0;
....
....
....
}
//if your offset depend upon 3rd segments of URL then
public function index(){
$offset = if($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
....
....
....
}
I wanted to help a friend on his website and he told me to change a link. he use smarty and the website is online
so i change this
<li class="first">Le réseau</li>
to absolute path.
Now, i have this error
Fatal error: Call to undefined function reecrire_url() in /home/deflandrgb/www/includes/smarty/sysplugins/smarty_internal_filter_handler.php on line 60
But I have not changed anything except link.
why I have this error?
Thanks
ps: I handed the old link but I still have the same error
EDIT : i find this function in Site_Smarty.class.php
class Site_Smarty extends Smarty{
public function Site_Smarty(){
parent::__construct();
//$this->caching = true;
//$this->compile_check = true;
$this->template_dir = root.'templates/';
$this->compile_dir = root.'templates_c/';
$this->config_dir = root.'includes/smarty/';
$this->cache_dir = root.'cache/';
//$this->debugging = true;
$this->registerFilter('pre','reecrire_url');
$this->assign('app_name', 'Guest Book');
$this->assign('path', path);
}
}
Seems you missed to define the callback function "reecrire_url"
See docs:
http://www.smarty.net/docs/en/api.register.filter.tpl
I am struggle with URI rerouting in my codeigniter version(2.2.0)application.
Here is my routes.php
$route['details/(:num)'] = 'agent/manage_agents/$1';
And in my view file
foreach($data as $value) {?>
Manage
}?>
But i will get 404 error.
In my Controller file
class Agent extends CI_Controller {
public function manage_agents($id)
{
echo $id;
}
}
UPDATE
Finally i found Which causes the problem.In my routes.php
$route['(:any)'] = "spotmyticket/$1"; when hide this line everything works fine.
Here is my complete routes.php
$route['404_override'] = '';
$route['default_controller'] = "spotmyticket";
$route['ticket']="ticket";
$route['ticket/(:any)'] = $route['ticket'].'/$1';
$route['captcha'] = "captcha";
$route['captcha/(:any)'] = "captcha/$1";
$route['admin'] = "admin";
$route['admin/(:any)'] = "admin/$1";
$route['userdashboard'] = "userdashboard";
$route['userdashboard/(:any)'] = "userdashboard/$1";
$route['fbci'] = "fbci";
$route['fbci/(:any)'] = "fbci/$1";
$route['(:any)'] = "spotmyticket/$1";
$route['agent-management'] = 'agent/index';
$route['register'] = "agent/agent_register";
$route['test'] = 'agent/test';
$route['details/(:any)'] = 'agent/manage_agents/$1';
Please define $route['(:any)'] = "spotmyticket/$1"; as the last rule in routes.php. Everything will be fine. This rule is for any url so all rules defined after this rule will be ignored.
If you are writing this route:
$route['details/(:num)'] = 'agent/manage_agents/$1';
so that you may pass any integer value (like you are passing $value['id']), then try using (:any) instead of (:num). So your route should be:
$route['details/(:any)'] = 'agent/manage_agents/$1';
Hi everyone im new with codeigniter. my routes.php is
$route['default_controller'] = "Maincontroller";
$route['(:any)'] = "Maincontroller/user_index/$1";
i want to search people if they type in the URL = www.site.com/username
but my problem is when going to other controller. should i route all my controllers?
$route['default_controller'] = "Maincontroller";
$route['somecontrollers'] = 'somecontrollers';
$route['(:any)'] = "Maincontroller/user_index/$1";
then how about my methods.
i tried this remap
public function _remap($method, $params = array())
{
if (method_exists(__CLASS__, $method)) {
$this->$method($params);
} else {
$this->user_index($method);
}
}
but this only works properly in controller which is not default, and i get the result i want. but as i apply this in my default controller it doesnt work well.
Hope this one help you!
$route['Maincontroller/(:any)'] = 'Maincontroller/user_index/$1';
$route['somecontrollers/(:any)'] = 'somecontrollers/user_index/$1';
$route['(:any)'] = "user_index/$1";
Hi i wont to make something like that.
http:// example.com/ - Main Controller
http:// example.com/rules/ - Main Controller where content get from database, but if not exist
return 404 page. (It's ok, isn't problem.)
But if i have subfolder in application/controlles/rules/
I want to redirect it to Main Contorller at Rules folder.
This follow code can solve problem, but i don't know how it right realise.
At routes.php:
$route['default_controller'] = "main";
$route['404_override'] = '';
$dirtest = $route['(:any)'];
if (is_dir(APPPATH.'controllers/'.$dirtest)) {
$route['(:any)'] = $dirtest.'/$1';
} else {
$route['(:any)'] = 'main/index/$1';
}
Ok, what I have:
controllers/main.php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
public function index($method = null)
{
if (is_dir(APPPATH.'controllers/'.$method)) {
// Need re-rout to the application/controllers/$method/
} else {
if ($query = $this->main_model->get_content($method)) {
$data['content'] = $query[0]->text;
// it shows at views/main.php
} else {
show_404($method);
}
}
$data['main_content'] = 'main';
$this->load->view('includes/template', $data);
}
}
Updated Again (routes.php):
So, seem's like what i search (work example):
$route['default_controller'] = "main";
$route['404_override'] = '';
$subfolders = glob(APPPATH.'controllers/*', GLOB_ONLYDIR);
foreach ($subfolders as $folder) {
$folder = preg_replace('/application\/controllers\//', '', $folder);
$route[$folder] = $folder.'/main/index/';
$route[$folder.'/(:any)'] = $folder.'/main/$1';
}
$route['(:any)'] = 'main/index/$1';
But, in perfectly need some like this:
http:// example.com/1/2/3/4/5/6/...
Folder "controllers" has subfolder "1"?
YES: Folder "1" has subfolder "2"?
NO: Folder "1" has controller "2.php"?
NO: Controller "controllers/1/main.php" has function "2"?
YES: redirect to http:// example.com/1/2/ - where 3,4,5 - parameters..
It is realy nice, when you have structure like:
http://example.com/blog/ - recent blog's posts
http://example.com/blog/2007/ - recent from 2007 year blog's posts
http://example.com/blog/2007/06/ - same with month number
http://example.com/blog/2007/06/29/ - same with day number
http://example.com/blog/web-design/ - recent blog's post's about web design
http://example.com/blog/web-design/2007/ - blog' posts about web design from 2007 years.
http://example.com/blog/current-post-title/ - current post
Same interesting i find http://codeigniter.com/forums/viewthread/97024/#490613
I didn't thoroughly read your question, but this immediately caught my attention:
if (is_dir($path . '/' . $folder)) {
echo "$route['$folder/(:any)'] = '$folder/index/$1';"; //<---- why echo ???
}
Honestly I'm not sure why this didn't cause serious issues for you in addition to not working.
You don't want to echo the route here, that will just try to print the string to screen, it's not even interpreted as PHP this way. There are also some issues with quotes that need to be remedied so the variables can be read as variables, not strings. Try this instead:
if (is_dir($path . '/' . $folder)) {
$route[$folder.'/(:any)'] = $folder.'/index/$1';
}
Aside: I'd like to offer some additional resources that are not directly related to your problem, but should help you nonetheless with a solution:
Preferred way to remap calls to controllers: http://codeigniter.com/user_guide/general/controllers.html#remapping
Easier way to scan directories: http://php.net/manual/en/function.glob.php
It's hard to say why the registering of your routes fails. From your code I can see that you're not registering the routes (you just echo them), additionally I see that the usage of variables in strings are used inconsistently. Probably you mixed this a bit, the codeigniter documentation about routes is not precise on it either (in some minor points, their code examples are not really syntactically correct, but overall it's good).
I suggest you first move the logic to register your dynamic routes into a function of it's own. This will keep things a bit more modular and you can more easily change things and you don't pollute the global namespace with variables.
Based on this, I've refactored your code a bit. It does not mean that this works (not tested), however it might make things more clear when you read it:
function register_dynamic_routes($path, array &$route)
{
$nodes = scandir($path);
if (false === $nodes)
{
throw new InvalidArgumentException(sprintf('Path parameter invalid. scandir("$path") failed.', $path));
}
foreach ($nodes as $node)
{
if ($node === '.' or $node === '..')
continue
;
if (!is_dir("{$path}/{$node}")
continue
;
$routeDef = "{$folder}/(:any)";
$routeResolve = "{$folder}/index/\$1";
$route[$routeDef] = $routeResolve;
# FIXME debug output
echo "\$route['{$routeDef}'] = '{$routeResolve}';";
}
}
$path = APPPATH.'controllers/';
register_dynamic_routes($path, $route);
$route['(:any)'] = 'main/index/$1';
Next to this you probably might not want to shift everything onto the index action, but a dynamic action instead. Furthermore, you might want to have a base controller that is delegating everything into the sub-controllers instead of adding the routes per controller. But that's up to you. The example above is based on the directory approach you outlined in your question.
Edit: Additional information is available in the Controllers section next to the URI Routing section
All this seems kind of complicated.
Plus, if you have hundreds (or thousands or more?) of possible routes in a database you may not want to load all of them into the "$routes" array every time any page loads in your application.
Instead, why not just do this?
last line of routes.php:
$route['404_override'] = 'vanity';
Controller file: Vanity.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Vanity extends MY_Controller {
/**
* Vanity Page controller.
*
*/
public function __construct() {
parent::__construct();
}
public function index()
{
$url = $_SERVER['PHP_SELF'];
$url = str_replace("/index.php/", "", $url);
// you could check here if $url is valid. If not, then load 404 via:
//
// show_404();
//
// or, if it is valid then load the appropriate view, redirect, or
// whatever else it is you needed to do!
echo "Hello from page " . $url;
exit;
}
}
?>