Welcome Guest, Not a member yet? Register   Sign In
How to pass argument to index(arg='') ?
#1

Hi,
It works well untill I change to new server.
I try to find out what i did wrong.
Here is the code

class Home extends MY_Controller {

/* this should load the index page */

// Dashboard

function __construct(){

parent::__construct();
}
public function index($team_id='')
{
....
}
}
before that I can access site by http://myweb.com/home.html or http://myweb.com/home/1.html
Now, I have to access only with index method http://myweb.com/home.html and http://myweb.com/home/index/1.html
How I can remove /index/
I wonder that why it works on my old server but after moving to cloud server it just does not work anymore.
Reply
#2

I got it to work by using _remap(). But I still wonder why I have to do this, while it was not need on previous server.
function __construct(){
parent::__construct();
$this->_remap($this->router->fetch_method());


}
function _remap($method){
if (method_exists($this, $method))
{
$this->$method();
}
else {
$this->index($method);
}
}
Reply
#3

If you want /home and /home/{{id}} you can always write a route in the application/config/routes.php file.

This is covered here http://www.codeigniter.com/user_guide/ge...ght=routes

So you could add for example:

$route['home/(:num)'] = 'home/index/$1';

DMyers
Reply
#4

(06-21-2016, 05:54 AM)dmyers Wrote: If you want /home and /home/{{id}} you can always write a route in the application/config/routes.php file.

This is covered here http://www.codeigniter.com/user_guide/ge...ght=routes

So you could add for example:

$route['home/(:num)'] = 'home/index/$1';

DMyers

This looks like we have to do for all controller classes if all controller classes have index($id='')
Reply
#5

You can also use the url_helper uri_segments() to get the url segments without specifying a parameter.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(06-21-2016, 06:40 AM)zenepay Wrote:
(06-21-2016, 05:54 AM)dmyers Wrote: If you want /home and /home/{{id}} you can always write a route in the application/config/routes.php file.

This is covered here http://www.codeigniter.com/user_guide/ge...ght=routes

So you could add for example:

$route['home/(:num)'] = 'home/index/$1';

DMyers

This looks like we have to do for all controller classes if all controller classes have index($id='')

Depending on the URI structure you are trying to create/recreate (ie. SEO reason, Business Reason, etc...) and because CodeIgniter uses the first 2 URI segments as the controller and methods (if you aren't using folders http://www.codeigniter.com/user_guide/ge...irectories). Then you will need to add the routes.

This is covered here: http://www.codeigniter.com/user_guide/ge...ur-methods

Assuming your URI structure isn't for any specific reason then why couldn't you use something like:

/people (of course default's to /people/index - list view)
/people/new (new record)
/people/edit/1 (edit record 1)
/people/delete/1 (delete record 1)
/people/view/1 (view record 1)
/people/details/1 (view record 1)

In the example above the Controller would be something like this:

class People extends CI_Controller {

public function index() {...}
public function new() {...}
public function edit($id=null) {...}
public function delete($id=null) {...}
public function view($id=null) {...}
public function details($id=null) {...}

}
Reply
#7

Thank you Dmyers,
Actually, the reason i asked because, it was working on my previous hosting server. But when I moved to new hosting with the same code, It just didn't work. I would think I might miss some configure or not.
I put the tail .html to the end of url.
Eg http://myweb.com/home.html which would view myself profile. Home is a controller and argument is id. This case it is default from login session for my own id.
Then when I view my friend profile it pass id to index($id='') the url is http://myweb.com/home/2.html
But this does not work anymore, I need to put longer like http://myweb.com/home/index/2.html
When to edit it will be like http://myweb.com/home/edit/2.html. I feel it jump away from a profile, where actually working on id 2.
Anyway, it is not a big deal, just find out if there is possibility to make it works witout having method /index/ in the url.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB