CodeIgniter Forums
How to Routes for Function with parameter? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: How to Routes for Function with parameter? (/showthread.php?tid=78006)



How to Routes for Function with parameter? - munggaran - 11-18-2020

I made module with following tree:

module\Blog
module\Blog\Config
module\Blog\Controller
module\Blog\View


I put Blog.php inside module\Blog\Controller with 2 below functions:


Code:
public function index()
{
  echo 'this is index!';
}

public function page($id)
{
    echo 'this is page = ' .$id;
}


and below Routes.php inside module\Blog\Controller

Code:
$routes->get('blog', '\Modules\Blog\Controllers\Blog::index');
$routes->get('blog/page/(:num)', '\Modules\Blog\Controllers\Blog::page\$1');


when I tried to execute http://localhost/blog ==> its working,
but when I try to trigger http://localhost/blog/page/1 ==> return 404 error 

How to declare the routes to pass Controller's function parameter the right way?


RE: How to Routes for Function with parameter? - vitnibel - 11-18-2020

Just replace last '\' to '/'

PHP Code:
$routes->get('blog/page/(:num)''\Modules\Blog\Controllers\Blog::page/$1'); 



RE: How to Routes for Function with parameter? - munggaran - 11-18-2020

(11-18-2020, 04:57 AM)vitnibel Wrote: Just replace last '\' to '/'

PHP Code:
$routes->get('blog/page/(:num)''\Modules\Blog\Controllers\Blog::page/$1'); 

Superb!!, thanksss  Big Grin Big Grin Big Grin been hours looking for hint, someone should put this as an example in the documentation.