Welcome Guest, Not a member yet? Register   Sign In
Controller with index($id) method with parameter
#1

Is it possible to have a Controller with the index() method that accepts a parameter?
e.g. controller called "Listjob" with method index($jobid) in such a way to allow this URL to work

http://myapp.local/listjob/53

I have no custom routes set in app/Config/Routes.php and router setup is default

PHP Code:
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true); 

The only way to make it work has been to abandon index($jobid) and just change its name from index to a different one , I used "show"
but now as you may guess I have to browse to

http://myapp.local/listjob/show/53

I also have tried to add a custom route

PHP Code:
$routes->add('listjob/(:any)''listjob/index/$1'); 

but again no success

Thank you for nay hint regarding this
Reply
#2

(This post was last modified: 08-25-2021, 07:06 AM by captain-sensible.)

i add a route for a "get request like"
Code:
$routes->get('(:any)', 'Pages::showme/$1');

Pages is a class I created and showme is one of class Pages methods.
SO in the first part of your route /listjob/$something is a url which for full would be something like : http://domain.com/listjob/$something

where $something represents a changing end bit of url

but on the right , it doesn't look as if your referencing any class or method

you could directly call method index method on a class if its define

but your route should be something like mine.

Quickest way to play would probably be to edit home controller and add a method



Try this
Code:
below : $routes->get('/', 'Home::index');

add route :

$routes->get('/(:any)', 'Home:test/$1');

Find the controller Home.php in app/Controllers and edit


add appropriately between class { and } :

Code:
public  function  test($something)

{
echo $something;
}


then on your url http://yourapp/vegetables

and see if "vegetables gets returned
Reply
#3

PHP Code:
  public function _remap()
    {
        return $this->index();
    
Reply
#4

(This post was last modified: 08-25-2021, 01:29 PM by Corsari.)

Thank you

yes, it works

thank to your hint , I went back to read the Routing section in the guide and the last two examples (like your) do the job
https://codeigniter.com/user_guide/incom...l#examples

and I've done like this

PHP Code:
$routes->add('jobs/(:num)''jobs::index/$1'); 

I was doing the route wrong instead of "::" I was using "/"
Reply
#5

(This post was last modified: 08-26-2021, 01:46 AM by captain-sensible.)

glad its working :: is a scope resolution operator and / makes it a path or url
Reply




Theme © iAndrew 2016 - Forum software by © MyBB