How can I load a module in Codeigniter with a special short name? something like this:
$this->load->module('LongModuleName', 'ShortName');
So that I can access module with:
$this->ShortName...
You would have to update the Thirdparty/MX/Loader.php
to look something like this:
/** Load a module controller **/
public function module($module, $params = NULL, $shortname=NULL) {
if (is_array($module)) return $this->modules($module);
if(isset($shortname))
$name = $shortname;
else
$name = $module;
$_alias = strtolower(basename($name));
CI::$APP->$_alias = Modules::load(array($module => $params));
return CI::$APP->$_alias;
}
Then to use it you would call:
$this->load->module('LongModuleName', NULL ,'Shortname');
The reason for the second parameter being null, is because this is looking for parameters
Related
I've working on User custom permission in Laravel 4, After login permissions(json string) stored in Auth::user()->permissions. as following:
$permissions =array(101,102);
DB::table('user')->where('id', Auth::user()->id)->update(['permissions' => json_encode($permissions)]);
But while I've checking permissions every time need to decode it to array:
if(in_array(1000, json_decode(Auth::user()->permissions)){
}
but I want something that make it to work like following:
if(in_array(1000, Auth::user()->usr_rights){
}
You can add a Accessor to your model :
public function getPermissionsAttribute($value)
{
return json_decode($value);
}
and all you have to do is this :
if(in_array(1000, Auth::user()->permissions){
}
You could use Implode
This isnt the nicest way of doing it but works.
Example:
$array = array('lastname', 'email', 'phone');
comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone
U could save this string in an additional row in user table.
and then convert it back with Explode
$pizza = "piece1, piece2, piece3, piece4, piece5, piece6";
$pieces = explode(",", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
In one of projects i got recently have all the urls like
www.abc.com/controller/action/1222
I want to encrypt the url parameters only to achieve something like
www.abc.com/controller/action/saddsadad232dsdfo99jjdf
I know I can do it by changing all the urls one by one and sending the encrypted parameters and dealing with them all over the places in the project.
So my question is Is there a way I can encrypt all the urls at once without making changes to every link one by one ?
Just need a direction.I guess I put all the details needed.
thanks !
Here is a solution if you use the site_url helper function and you are sure that all your URLs comply this format www.abc.com/controller/action/1222:
All you need is to override the site_url method of the CI_Config class
class MY_Config extends CI_Config
{
public function site_url($uri = '', $protocol = NULL)
{
$urlPath = ltrim(parse_url($this->_uri_string($uri), PHP_URL_PATH), '/');
$segments = explode('/', $urlPath);
$numOfSegments = count($segments);
$result = [$segments[0], $segments[1]]; // controller and action
// start from the third segment
for($i = 2; $i < $numOfSegments; $i++)
{
// replace md5 with your encoding function
$result[] = md5($segments[$i]);
}
return parent::site_url($result, $protocol);
}
}
Example:
echo site_url('controller/action/1222'); will outputwww.abc.com/controller/action/3a029f04d76d32e79367c4b3255dda4d
I use a Hashids helper. I think this will do what you're after.
You can pass parameters to your functions like this:
base_url('account/profile/' . hashids_encrypt($this->user->id))
You can then decrypt it within the function and use it however you want:
$id = hashids_decrypt($id);
I am trying to load a page dynamically based on the database results however I have no idea how to implement this into codeigniter.
I have got a controller:
function history()
{
//here is code that gets all rows in database where uid = myid
}
Now in the view for this controller I would like to have a link for each of these rows that will open say website.com/page/history?fid=myuniquestring however where I am getting is stuck is how exactly I can load up this page and have the controller get the string. And then do a database query and load a different view if the string exsists, and also retrieve that string.
So something like:
function history$somestring()
{
if($somestring){
//I will load a different view and pass $somestring into it
} else {
//here is code that gets all rows in database where uid = myid
}
}
What I don't understand is how I can detect if $somestring is at the end of the url for this controller and then be able to work with it if it exists.
Any help/advice greatly appreciated.
For example, if your url is :
http://base_url/controller/history/1
Say, 1 be the id, then you retrieve the id as follows:
function history(){
if( $this->uri->segment(3) ){ #if you get an id in the third segment of the url
// load your page here
$id = $this->uri->segment(3); #get the id from the url and load the page
}else{
//here is code that gets all rows in database where uid = myid and load the listing view
}
}
You should generate urls like website.com/page/history/myuniquestring and then declare controller action as:
function history($somestring)
{
if($somestring){
//I will load a different view and pass $somestring into it
} else {
//here is code that gets all rows in database where uid = myid
}
}
There are a lot of ways you can just expect this from your URI segments, I'm going to give a very generic example. Below, we have a controller function that takes two optional arguments from the given URI, a string, and an ID:
public function history($string = NULL, $uid = NULL)
{
$viewData = array('uid' => NULL, 'string' => NULL);
$viewName = 'default';
if ($string !== NULL) {
$vieData['string'] = $string;
$viewName = 'test_one';
}
if ($uid !== NULL) {
$viewData['uid'] = $uid;
}
$this->load->view($viewName, $viewData);
}
The actual URL would be something like:
example.com/history/somestring/123
You then know clearly both in your controller and view which, if any were set (perhaps you need to load a model and do a query if a string is passed, etc.
You could also do this in an if / else if / else block if that made more sense, I couldn't quite tell what you were trying to put together from your example. Just be careful to deal with none, one or both values being passed.
The more efficient version of that function is:
public function history($string = NULL, $uid = NULL)
{
if ($string !== NULL):
$viewName = 'test_one';
// load a model? do a query?
else:
$viewName = 'default';
endif;
// Make sure to also deal with neither being set - this is just example code
$this->load->view($viewName, array('string' => $string, 'uid' => $uid));
}
The expanded version just does a simpler job at illustrating how segments work. You can also examine the given URI directly using the CI URI Class (segment() being the most common method). Using that to see if a given segment was passed, you don't have to set default arguments in the controller method.
As I said, a bunch of ways of going about it :)
Here is example of URl_title CI, i know this code is do this
$title = "Whats wrong with CSS";
$url_title = url_title($title, '_', TRUE);
// Produces: whats_wrong_with_css
But hot to revers, is there a function in Ci to reverse something like this and return the true value?
like this ?
// Produces: Whats wrong with CSS
hi you can do it just with simple way
$title = ucfirst(str_replace("_",' ',$url_tilte));
echo $title;
I would "extend" CI's URL helper by creating a MY_url_helper.php file in application/helpers and create a function similar to what umefarooq has suggested.
/*
* Un-create URL Title
* Takes a url "titled" string as de-constructs it to a human readable string.
*/
if (!function_exists('de_url_title')) {
function de_url_title($string, $separator = '_') {
$output = ucfirst(str_replace($separator, ' ', $string));
return trim($output);
}
}
Providing you have loaded the url helper, you will then be able to call this function throughout your application.
echo de_url_title('whats_wrong_with_css'); // Produces: Whats wrong with css
The second ($separator) paramater of the function allows you to convert the string dependent on whether it's been "url_title'd" with dashes - or underscores _
When calling a function within a class I am using something like this:
protected $_mdl = 'mdl_posts_latest';
function __construct()
{
parent::__construct();
$this->load->model($this->$_mdl);
}
public function index()
{
$offset = 0;
$limit = 5;
$data['p_latest'] = $this->mdl_posts_latest->get_posts_latest($offset, $limit);
...
...
...
.
and it is working.
The problem is when I ry something like:
$data['p_latest'] = $this->$this->$_mdl->get_posts_latest($offset, $limit);
It throws this error:
Object of class Posts_latest could not be converted to a string
because, obviously, this code is wrong: $this->$this->$_mdl->
So, my question is how can I define the name of my modeljust once at the top of my class and then use it as a variable within all calls for calling a function etc.
Because right now I don't know how to do it so it would look like something:
$this->$model->get_something();
When you're loading the model, you should access the member variable like this, without the $ preceding the member variable's name:
$this->load->model($this->_mdl);
You could look into PHP's variable variables and complex (curly) syntax. It would allow you do something like this, where you can use the value of the variable:
$this->{'_mdl'}->get_posts_latest($offset, $limit);
$this->load->model($_mdl);
instead of
$this->load->model($this->$_mdl);
then:
$data['p_latest'] = $this->$_mdl->get_posts_latest($offset, $limit);
or
$md1 = $this->$_md1;
$data['p_latest'] = $md1->get_posts_latest($offset, $limit);