Codeigniter 4 Current/Active Controller - codeigniter

In Codeigniter 3 is possible to get current active class and method with this code:
$active_controller = $this->router->fetch_class();
$active_function = $this->router->fetch_method();
Are there such functions in Codeigniter 4?

In CodeIgniter 4
$router = service('router');
$controller = $router->controllerName();
$router = service('router');
$method = $router->methodName();

Its worth saying those classes were never officially part of CI3 (https://codeigniter.com/user_guide/installation/upgrade_300.html?highlight=fetch_class). Bearing in mind CI4 is a lot more flexible and that routes are defined more variably I would look at the routing side of things and extract it from there (https://codeigniter4.github.io/userguide/incoming/incomingrequest.html#the-request-url).

You can use PHP constant or functions that provide you the same:
Get Current Function name:
__FUNCTION__
Get Current Class name:
__CLASS__
OR
get_class()
Codeigniter 4: All the above is working well otherwise use the same code that #mathan answered:
$router = service('router');
echo $router->controllerName();

Related

Routing in Codeigniter (:any) gives me strange results

Here i'm using below routing config
$route['controllername/(:any)'] = "controllername/index/$1";
this is working well for me, but i want to use other methods in the same controller.
for that i'm using below route
$route['controllername/search'] = "controllername/search";
this is also working fine, but i want pass parameters to this method.
Here if i'm passing parameters but it is calling index method
i want to use both of above routes, i have tried with below route also but same result
$route['controllername/search/(:any)'] = "controllername/search/$1";
could anyone give any suggestions?
Thankyou!!
Ok.Do it like this for routing multiple functions in same controller :
$route['default_controller'] = "controller_name";
$route['index/(:any)'] = "controller_name/index/$1";
$route['search/(:any)'] = "controller_name/search/$1";
Let me know if you find any issues or doubts.
#user4039421
change your roouting rules position like below
$route['controllername/search/(:any)'] = "controllername/search/$1";
$route['controllername/search'] = "controllername/search";
$route['controllername/(:any)'] = "controllername/index/$1";

Joomla 3 - How to get value from configuration file?

I'm building a custom component and I just want to get a value from the global config in my controller. I can't find any information about how to do this.
Something like...
$config = JFactory::getConfig();
$this->_db = $config->get('db');
The documentation on how to do it is slightly outdated:
http://docs.joomla.org/JFactory/getConfig
But if you check the code they actually drop the ampersand function:
https://github.com/joomla/joomla-cms/blob/staging/components/com_users/models/registration.php
$config = JFactory::getConfig();
$fromname = $config->get('fromname');
Also if you are trying to connect to the database you really can just use the DB object from JFactory.
$db = JFactory::getDbo();
Learn more about properly connecting to the database here:
http://docs.joomla.org/Accessing_the_database_using_JDatabase
Since Joomla 3.2:
JFactory::getApplication()->get($varname, $default);
See the reference

Using X-Editable - Backend part

So I'm just trying to use xeditable (http://vitalets.github.io/x-editable/docs.html#gettingstarted) to make changes to my database via AJAX.
Since I'm new to this concept and I'm (forcefully) working with PHP for the first time, I need some help.
I setup the frontend part, and a script called (say) script.php is handling the data for me (I need to write the new value in my database).
I can't really understand what to do in the script. Can someone guide me towards it? The docs above don't really do it for me.
Looking in a project I worked on a few months back (sorry about the mysql_ stuff – not my choice!)
Something like:
<?
include your/database/connection_stuff.php;
// Can't remember if x-editable passes the table in as well or not
$table = mysql_real_escape_string($_GET['table']);
// If not,
$table = 'name_of_table';
$value = mysql_real_escape_string($_POST['value']);
$name = mysql_real_escape_string($_POST['name']);
$pk = mysql_real_escape_string($_POST['pk']);
$result = mysql_query("UPDATE `$table` SET `$name` = '$value' WHERE id = '$pk'");
?>
Will do the trick.

Laravel 4: how can I understand how it all works?

I am using Laravel 3 in one project and it's been a joy. I have also looked at the source code several times to see how some things work behind the scenes.
But now in Laravel 4, I don't know where to begin or how to understand it all. Where can I learn all the behind the scenes of Laravel 4?
Case in point: I wanted to find out if the DB::insert() returns the id of inserted row. So I started searching.
1. I found the Illuminate\Support\Facades\Facade class that "encapsulates" DB.
2. The resolveFacadeInstance function is called and then I tried to print these arrays, but my computer hangs :-/. And I'm sure this would lead to many more classes that I wouldn't understand.
Is there a way I could try to learn the inner workings of Laravel 4? Maybe stack traces?
The facade class is just a filter class to allow you to call methods as if they were static.
For the facade mappings go here: http://laravel.com/docs/facades#facade-class-reference
The starting point to fully understand laravel's inner-workings should begin at:
/public/index.php
You can follow the logic of the program, noticing that requires start.php, which loads an instance of the "Application" which is found here:
/vendor/laravel/framework/src/Illuminate/Foundation/Application.php
This Tuts+ video shows a couple of ways of finding out what class is actually doing the work.
E.g.:
$root = get_class(DB::getFacadeRoot());
var_dump($root);
You can check out the early docs for Laravel 4 here : http://four.laravel.com/ – that should give you a good starting point
The actual Laravel 4 code is well documented in the files. If you want to understand the inner workings then open up the source code files and read the notes. For example I looked up the DB::insert() code in /vendor/laravel/framework/src/Illuminate/Foundation/Application.php.
/**
* Run an insert statement against the database.
*
* #param string $query
* #param array $bindings
* #return bool
*/
public function insert($query, $bindings = array())
{
return $this->statement($query, $bindings);
}
Ok, so this is calling the statement function so I search for function statement in the same code / class:
/**
* Execute an SQL statement and return the boolean result.
*
* #param string $query
* #param array $bindings
* #return bool
*/
public function statement($query, $bindings = array())
{
return $this->run($query, $bindings, function($me, $query, $bindings)
{
if ($me->pretending()) return true;
$bindings = $me->prepareBindings($bindings);
return $me->getPdo()->prepare($query)->execute($bindings);
});
}
We can now see that this returns the boolean result based on the comments above the code.
If you come from Laravel 3 this article is for you. After that you should read the other tutorials of that series.
Author's note:
This article should outline some of the more important changes to Laravel between versions 3 and the upcoming version 4. Bear in mind
this isn’t all of the changes. As the release of Laravel 4 gets closer
I’ll keep this article up to date. If you’re having any problems with
Laravel 4 please jump on to #laravel on Freenode. At this time we’d
like to ask people not to post help topics on the forums.

CodeIgniter: current_url shows question mark

Say my page is on:
http://localhost/app1/profile/index/123/
The result of current_url() is this:
http://localhost/app1/?profile/index/123
There is a ? that shouldn't be there. The question mark seems to be caused by the following config setting:
$config['enable_query_strings'] = TRUE;
I need query strings enabled in my application. Any ideas what I need to do?
EDIT 1:
Also, in the case when the URL does have a query string, I need current_url to also return that. I'm hoping Phil Sturgeon's solution here CodeIgniter current_url doesn't show query strings will help me.
I'm using CI 2.1.0.
As mentioned, $config['enable_query_strings'] is sort of a "legacy" setting in Codeigniter from back when there was no support $_GET (really, none).
http://codeigniter.com/user_guide/general/urls.html
Enabling Query Strings
In some cases you might prefer to use query strings URLs:
index.php?c=products&m=view&id=345
c === Your controller name
m === Your method name
The rest is the method arguments. It's a very misleading description, and there's no mention of the other setting or query strings at all in the rest of the URL docs. I've never heard of anyone actually using this. CI comes with $config['allow_get_array']= TRUE; by default, which is what you want.
You can modify the current_url() function for query string support, just create application/helpers/MY_url_helper.php and use this:
function current_url($query_string = FALSE)
{
$CI =& get_instance();
$current_url = $CI->config->site_url($CI->uri->uri_string());
// BEGIN MODIFICATION
if ($query_string === TRUE)
{
// Use your preferred method of fetching the query string
$current_url .= '?'.http_build_query($_GET);
}
// END MODIFICATION
return $current_url;
}
Then call it like current_url(TRUE) to include the query string.
Don't use: $config['enable_query_strings'] = TRUE;
Use this instead: $config['allow_get_array']= TRUE;
enable_query_strings is not what you think and is not used much.
To build your own query strings, use one of these two:
$query_string = http_build_query($this->input->get());
$query_string = $this->input->server('QUERY_STRING');
along with this:
$lastseg_with_query = $lastseg.'?'.$query_string;
Please consult this SO Q&A for more information: URI Segment with Question Mark

Resources