Currently my URL is domain.de/site/home, domain.de/site/about.
But i want that my URL ist domain.de/home, domain.de/about etc.
I already have a really simple Controller. In this Controller i`ve a function for each Site:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home");
$this->load->view("site_footer");
}
public function about() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about");
$this->load->view("site_footer");
}
}
Open application/config/route.php and add following route path
$route['home'] = "site/home";
$route['about'] = "site/about";
You can define the association between a URL pattern and a controller/action in your config/routes.php file. Read the docs here and you will understand how it can be done.
By default, your routes will be {domain}/{controller_name}/{action_name} unless you override that in routes.php where you can assign to your controller/action a different URL pattern.
So probably in your case you would need something along the lines of:
$route['home'] = 'site/index';
$route['about'] = 'site/about';
Even better you might want to set your site controller as default controller:
$route['default_controller'] = "site";
Then all requests starting from the base url will get directed to actions in your site controller. That is {domain}/{action_name} will be directed to site/action_name automatically.
Related
in codeigniter I have my main controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller
{
public function index()
{
$this->load->library('../controllers/forum');
$obj = new $this->forum();
$obj->test();
}
}
And the controller I'm trying to access:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Forum extends CI_Controller
{
function __construct()
{
echo "testing1";
$this->load->library('session');
parent::__construct();
$this->load->database();
$this->load->model('model_forum');
}
public function index(){
}
public function test(){
echo "testing2";
$this->data['forums'] = $this->model_forum->getForums();
$this->load->view('homepage', $this->data);
}
}
Everything is fine with my model_forum.php file, because it works if I put all the code in Main controller. But if I'm trying to access Forum controller, nothing works, only "testing1" echo goes through. Picture of error:
Anyone has any idea what I'm doing wrong? I'm new to PHP and codeigniter so I'm struggling a little bit. Thanks in advance.
You can't load a controller from a controller in CI - unless you use HMVC or something.
You should think about your architecture a bit. If you need to call a controller method from another controller, then you should probably abstract that code out to a helper or library and call it from both controllers.
UPDATE
After reading your question again, I realize that your end goal is not necessarily HMVC, but URI manipulation. Correct me if I'm wrong, but it seems like you're trying to accomplish URLs with the first section being the method name and leave out the controller name altogether.
If this is the case, you'd get a cleaner solution by getting creative with your routes.
For a really basic example, say you have two controllers, controller1 and controller2. Controller1 has a method method_1 - and controller2 has a method method_2.
You can set up routes like this:
$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";
Then, you can call method 1 with a URL like http://example.com/method_1 and method 2 with http://example.com/method_2.
Albeit, this is a hard-coded, very basic, example - but it could get you to where you need to be if all you need to do is remove the controller from the URL.
You could also go with remapping your controllers.
From the docs: "If your controller contains a function named _remap(), it will always get called regardless of what your URI contains.":
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Here is my pages.php:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function index()
{
$this->load->view('home');
}
public function event()
{
$this->load->view('event');
}
public function envi()
{
$this->load->view('envi');
}
public function tour()
{
$this->load->view('tour');
}
Can you guys help me out when click on button localhost/social/pages/envi this is opens.. but i need that file should be access and url should localhost/envi how to do that can u guys help me
There are number of ways for doing this
Like you can set apache rule in .htaccess file OR
Define your routing in this file
application/config/routes.php
Put this line of code in routes.php
$route ['envi'] = "social/pages";
URL will go like this
http://localhost/envi
Hi ShanmukSrinivas Gandepalli,
There a file routes.php in the config folder, open it add define your action here
Like below, here home is controller name and aboutus is method name. Now you can call this by aboutus no need to add contoller in the url.
$route['aboutus'] = 'page/aboutus';
Hope this will help you!
I have some issues with Routes in CI. also new with this framework. I want to ask you guys, (I have read similar example with my problem but it doesn't work for me).
(the link)
Codeigniter routing issues
every time I want to link another page in main page. CI said:![error routes][1]
An Error Was Encountered
Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.
when i access this url in my browser: /test, i get the error routes.
here is a sample of my routes.php:
$route['default_controller'] = "frontline";
$route['404_override'] = 'error404';
$route['test'] = 'frontline/test';
and this my controller:
class Frontline extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('index.php');
}
public function test(){
echo "test";
}
}
I have various static pages and I dont want to create independent controller for them all.
Say I want to create a controller named page and have all of the static pages as functions.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Page extends Frontend_Controller {
public function index()
{
}
public function store(){
$this->load->view('public/store');
}
public function contact(){
$this->load->view('public/contact');
}
public function about(){
$this->load->view('public/about');
}
}
Is there anyway to modify the URI routes so that I wouldnt have to type mydomain.com/page/contact but I could type mydomain.com/contact to view the page
In your application/config/routes.php
You can add as:
$route['contact'] = "page/contacts";
$route['store'] = "page/store";
...
In my application/config/routes.php I use the following regex to do that. Most sites I work on are relatively small so I list all of the controllers in the following snippet (because it's easier than listing all of the static pages)
$route['^(?!controller1|controller2).*'] = "page/$1";
The logic basically says that If the path does not start with controller1 or controller2 then use page as the controller for urls like http://domain.tld/page
For not having to create 10+ static routes for all my static pages it keeps it pretty simple.
I have set up a folder with a controller in it: controllers/admin/home.php, but I get a 404 from the browser when I try to access it.
This is my routes file:
$route['employers'] = "employers/home";
//$route['employers/dash'] = "employers/dash";
$route['default_controller'] = "home";
$route['404_override'] = '';
This is the controller file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home extends CI_Controller {
function __construct(){
parent::__construct();
/*
enable profiler
*/
//$this->output->enable_profiler(TRUE);
$this->load->helper('url');
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('layout');
}
}
.htaccess seems fine standard. Any ideas on what i'm doing wrong?
Note some things:
1) Routes are executed in the order they're written, and your custom routes MUST follow the default ones. So, it should be:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['employers'] = "employers/home";
This if your controller "home" is inside the folder "employers".
2) Controllers don't need all that stuff you wrote, indeed you don't even need to call the parent constructor unless you're planning to load libraries and resource for the whole controller's methods (which can be achieve also by autoloading them in the autoload.php file), so it could simply be:
file: application/controllers/employers/home.php
class Home extends CI_Controller {
function index()
{
// this is the method you're calling with your URL!
}
}
3) As per above, and as already pointed out by #Wesley, with your url you're trying to access the INDEX method of your controller HOME in your subfolder EMPLOYERS. But you didn't defined an index() method (which is the one called by default if no other is supplied).
It seems, instead, that CI is trying to look for an employers controller and a home method; if it doesnt find it, but you have a employers folder, it tries to access the index method in the home controller in the employers folder. And, since it didn't find it either, you're getting the 404 page.
Hope I'm clear, otherwise just ask.
You failed to say how you were trying to access it via url. It should be:
{YOUR_BASE_URL}admin/home
... followed by optional URL segments (/method/param1/param2/etc).
Without the additional segments, this would by default load the index method. However, since you don't have any methods defined, there's nothing to load.
If this still fails after you define a method, move the controller file out of the sub-directory for starters, and make sure it works.