How to load an index page without a controller - codeigniter

I have a question and apologies if it is too simple but I really couldn't figure it out. If I have my domain for example: www.shop.com. I changed .htaccess to omit the need for index.php. Now I am wondering how can I load for example a page (main home page) without having any controller in url address. E.g.:
www.shop.com =========> should land me on the home page of my site.
currently the only way I can do it is by defining a controller and doing this:
www.shop.com/controller/
Thanks and your help is much appreciated :)

Go to /application/config/routes.php
Add the default_controller rule like so
$route['default_controller'] = 'home';
So now in /application/controllers/home.php method index() will run on the index page.

Related

Can't link to an anchor on another page in codeigniter

From the research I've done this code should work. But for some reason I keep getting the error that the page is not found. Maybe it's because it's going though my Main_controller?
public function privacy($page='privacy') {
$this->load->view($page);
}
The VIEW:
Privacy Policy<span class=""></span>
The idea is have the privacy popup from anywhere I would want to link to it. Right now I have to put the text on the same page and then link to it through the id="myModal-privacy".
Thanks for any and all input.
The codeigniter url convention is: "mysite.com/controller/function/id. see docs
your link contains a hashtag, which is not working as id like you intend, change it to the CI-convention like this:
Privacy Policy<span class=""></span>
which will now send in the url myModal-privacy as id and you'd load this page in the controller:
$this->load->view('myModal-privacy');
In your question controller name is Main_Controller and in the view, you are using main.
If you are not defining route then use same controller name to call the function
Privacy Policy<span class=""></span>
Did you forget to add a question mark?
Correct Code
Privacy Policy<span class=""></span>

CodeIgniter - adds an extra class to my URL when i click on a link

I'm new in CI as well in php. I have a problem that's bugging me for two days now:
when i click on a link in my admin header(say: Articles) it takes me to: www.example.com/admin/articles, which is ok. If now i try to click on another link in the header (say: Add articles), the url becomes: www.example.com/admin/admin/add_articles - it adds an extra admin to my url. if i click again on Articles, the url will be: www.example.com/admin/admin/admin/articles, and so on.
Do you have any idea why is this happening?
Thanks
You have 2 choice, the first is that you wrote in every link base_url()
OR
you can use a built in helper:
anchor('route','label','attributes')
in your example:
anchor('admin/add_article','Add an article',array('class' => 'link'))
Then that will create this HTML code:
Add an article
use absolute urls not relative, use $config['base_url'] before every link
Don't use
$config['base_url] . 'controller/action',
use the function:
site_url('controller/action');
Or use the anchor function #András Rátz suggested.

How do I edit the index page of your domain in CodeIgniter?

So right now I have this in my .htaccess page:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|js|fonts|css|robots\.txt)
RewriteRule ^(.+)$ index.php?$1 [L]
which makes it so that I can access http://www.yourdomain.com/index.php/page by going to http://www.yourdomain.com/page. However what I can't figure out is how to edit the page that shows up at http://www.yourdomain.com.
I've tried creating a controller called index, but this doesn't do anything unless you go to http://www.yourdomain.com/index
Can anyone help with this? Thank you!!!
The default controller that loads when there are no url segments is defined in application/config/routes.php:
From: http://codeigniter.com/user_guide/general/routing.html
Reserved Routes
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the
URI contains no data, which will be the case when people load your
root URL. In the above example, the "welcome" class would be loaded.
You are encouraged to always have a default route otherwise a 404 page
will appear by default.
If there are no url segments besides the controller itself, by default the index() method of that controller is called (this is always the case, not just in regards to routing). So with this example you would look at the welcome controller and index method and see which view files etc. are being loaded.

loading view in codeigniter

HI I have a codeigniter controller called CIcontroller and I have a method say redirectmethod
in the redirectmethod i have some code and then i do this
$data['redirect_page'] = 'page_name';
$this->load->view('template_view',$data);
the template view basically loads header footer and the corresponding view as specified by the data parameter
Now everything works fine but my url has value http:\\blabla\CIcontroller\redirectmethod instead of http:\\blabla\page_name
could anyone help me fix this thing
You need to emit a Location header to tell the browser to load a different page. See redirect in the url helper.

CodeIgniter Url Rewrite (language dependent)

So, i have my .htaccess, my controllers, everything is going fine. I added localization, so now i have Portuguese(Default), English and Italian.
I am using the _lang files in the appplication/languages directory, i am using session->userdata('lang') and everything works fine.
My controllers are named with portuguese words, after the top menu. What i'm looking for is:
to rewrite my url, changing the name of the controller, depending on the session->userdata('lang').
Is this even possible? how?
Thank you
So i am trying, as InFog suggested, in the routes file:
if ($this->session->userdata('lang') == 'english') {
$route['novidades/([a-z]+)'] = 'news/$1';
}
but i just get a blank screen when i open the application.
And i've tried it without the if clause, and nothing happens, when i go to
http://localhost/myapp/novidades
the url stays the same
You can solve this using CodeIgniter Routes. You can do it editing the file 'system/application/config/routes.php:
$route['news/([a-z]+)'] = 'noticias/$1';
This way an URL like '/news/run-fools' will be remaped to 'noticias/run-fools'. Now you can have just one controller =)
Good Luck
Override CI_Router to translate the name in the fetch_class() method to change controllers. Override fetch_method() to change methods.

Resources