CodeIgniter: jak uzyskać informacje o kontrolerze, akcji, adresie URL
Mam te adresy URL:
Jak uzyskać nazwę kontrolera, nazwę akcji z tych adresów URL. Jestem CodeIgniter newbie. Czy istnieje jakaś funkcja pomocnicza do uzyskania tej informacji
Ex:
$params = helper_function( current_url() )
Gdzie $params
staje się czymś w rodzaju
array (
'controller' => 'system/settings',
'action' => 'edit',
'...'=>'...'
)
11 answers
Możesz użyć klasy URI :
$this->uri->segment(n); // n=1 for controller, n=2 for method, etc
Powiedziano mi również, że następujące prace, ale obecnie nie jestem w stanie przetestować:
$this->router->fetch_class();
$this->router->fetch_method();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-17 14:46:26
Zamiast używać segmentów URI powinieneś to zrobić:
$this->router->fetch_class(); // class = controller
$this->router->fetch_method();
W ten sposób wiesz, że zawsze używasz prawidłowych wartości, nawet jeśli jesteś za przekierowanym adresem URL, w sub-domenie itp.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-15 11:44:57
Metody są przestarzałe.
$this->router->fetch_class();
$this->router->fetch_method();
Możesz uzyskać dostęp do właściwości.
$this->router->class;
$this->router->method;
Zobacz codeigniter user guide
Metody routingu URI fetch_directory(), fetch_class (), fetch_method ()
Z właściwościami
CI_Router::$directory
,CI_Router::$class
orazCI_Router::$method
bycie publicznym i ich odpowiedniefetch_*()
nie dłużej robiąc cokolwiek innego, aby po prostu zwrócić właściwości - nie zachowaj je.To wszystkie wewnętrzne, nieudokumentowane metody, ale zdecydowaliśmy się na wycofaj je na razie, aby zachować kompatybilność wsteczną na wszelki wypadek. Jeśli niektórzy z was z nich skorzystali, to teraz możecie po prostu dostęp do właściwości:
$this->router->directory; $this->router->class; $this->router->method;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-30 10:44:02
Another way
$this->router->class
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-04 12:57:03
Jako dodatek
$this -> router -> fetch_module(); //Module Name if you are using HMVC Component
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-03 10:24:45
Update
Odpowiedź została dodana w 2015 roku i następujące metody są obecnie przestarzałe
$this->router->fetch_class(); in favour of $this->router->class;
$this->router->fetch_method(); in favour of $this->router->method;
Witam powinieneś użyć następującego podejścia
$this->router->fetch_class(); // class = controller
$this->router->fetch_method(); // action
W tym celu, ale aby go użyć, musisz przedłużyć hak z CI_Controller
i działa jak urok, nie powinieneś używać segmentów uri
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-12-11 16:03:55
Jeśli używasz $ this->uri->segment, jeśli zmienią się reguły przepisywania adresów URL, dopasowanie nazw segmentów zostanie utracone.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-01-24 11:20:07
Użyj tego kodu w dowolnym miejscu w klasie lub bibliotekach
$current_url =& get_instance(); // get a reference to CodeIgniter
$current_url->router->fetch_class(); // for Class name or controller
$current_url->router->fetch_method(); // for method name
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-20 08:27:42
Ostatni segment URL zawsze będzie akcją. Proszę o tak:
$this->uri->segment('last_segment');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-02-14 10:49:36
$this->router->fetch_class();
/ / klasa fecth klasa w kontrolerze $this - > router - >fetch_method ();
// Metoda
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-13 03:54:20
Klasa kontrolera nie działa z żadnymi funkcjami.
Dlatego polecam Wam korzystanie z następujących skryptów
global $argv;
if(is_array($argv)){
$action = $argv[1];
$method = $argv[2];
}else{
$request_uri = $_SERVER['REQUEST_URI'];
$pattern = "/.*?\/index\.php\/(.*?)\/(.*?)$/";
preg_match($pattern, $request_uri, $params);
$action = $params[1];
$method = $params[2];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-05 03:55:19