CodeIgniter Forums
how to get string before the second slash as a parameter?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: how to get string before the second slash as a parameter?? (/thread-70502.html)



how to get string before the second slash as a parameter?? - imalegend - 04-18-2018

I'm working on a project ...

I want to have my institutions and get the string before second slash as a institution username

for example:

in this path:

http://married.org/institution1/class/method/

I want to get the institution1 as a parameter to detect this institution and show this it's page and informations.

so I want to write my controller between second slash and third slash

and between third slash and fourth slash I want to write my method and parameter comes after them...

what do I do??

I guess should user the htaccess, but don't know how to do!


RE: how to get string before the second slash as a parameter?? - tomdchi - 04-18-2018

Look at the URI library: https://www.codeigniter.com/user_guide/libraries/uri.html

This should do what you want:


PHP Code:
$segment $this->uri->segment(1); 



RE: how to get string before the second slash as a parameter?? - skunkbad - 04-18-2018

Look at the URI class. It has what you need, specifically the segment method.


RE: how to get string before the second slash as a parameter?? - imalegend - 04-19-2018

Thank you my friends , But I think this way not work!

Because...

I know how to get url and it's segments by this class(URI)!

But I don't know how to change url analysing in codeigniter. In another words I want to codeiginter get first parameter as a $_GET parameter and second parameter as a controller and third parameter as a method and next parameter as another parameters.

Like this example:

http://hamresan.ir/intstitution1/search/add/ ... (and others as parameters)
"institution1" as a parameter like center (center=institution1)
"search" as a controller
and "add" as a method

But by default setting of codeigniter I have to get like this, but I don't want:

http://hamresan.ir/intstitution1/search/add/

"institution1" as a controller
"search" as a method
and "add" as a parameter

But I don't want this Sad

please help me..


RE: how to get string before the second slash as a parameter?? - ciadmin - 04-19-2018

You could use a routing rule, eg something like $route['(:any)/(:any)/(:any)'] = '$2/$1/$3';
See https://www.codeigniter.com/user_guide/general/routing.html


RE: how to get string before the second slash as a parameter?? - imalegend - 04-19-2018

(04-19-2018, 01:42 AM)ciadmin Wrote: You could use a routing rule, eg something like $route['(:any)/(:any)/(:any)'] = '$2/$1/$3';
See https://www.codeigniter.com/user_guide/general/routing.html

hi my friend

I tried to do what you say , but I couldn't to do that...

and I user this code in route.php :

$route['([a-zA-Z]+)'] = '?center=$1';

and

$route['([a-zA-Z]+)'] = 'index.php/?center=$1';

can you help me?


RE: how to get string before the second slash as a parameter?? - imalegend - 04-19-2018

$route['(:any)'] = 'login/index/?center=$1';

it's works but don't get the center as a $_GET parameter

altough absolute address work and get the center as a $_GET parameter!:

http://localhost/hamresan/login/index/?center=myinstitution

the get center=myinstitution