What's the difference between "(:any)" and ":any" in CodeIgniter URI routing rules? For example:
segment_1/segment_2/:any = my_controller/function/$1
And
segment_1/segment_2/(:any) = my_controller/function/$1
I don't see an explanation in the CI docs and wondered. :)
There is difference between :any and (:any).
First (:any) replaced with $1 second (:any) replaced with $2 and so on
but :any does not have any effect.
As an example,Suppose you have a test controller with a function name myfunction takes an arguemnt $a like this
class Test extends CI_Controller
{
public function myfunction($a='')
{
echo $a;
}
}
Hit this url baseurl/test/asdf
$route['test/(:any)']='test/myfunction/$1';
//$1== asdf
//outputs asdf
$route['test/:any']='test/myfunction/$1';
//$1!=asdf
//outputs $1
Hope you understand the difference.
Related
I am using a route with 2 optional parameters and I would like that either one of them can be chosen because they are used in a where clause. The where clause can be used on the first parameter OR the second one.
I tried this:
Route::get('activityperemployee/employee_id/{employee_id?}/month/{month?}', ['uses'=>'Ajax\ActivityAjaxController#getActivityPerEmployee','as'=>'ajaxactivityperemployee']);
but the problem is I can't find the page anymore if I don't set up both of the parameters.
The problem is the first parameter {employee_id?}. You cannot use it in this way becasue if You won't pass any params You'll get url like:
activityperemployee/employee_id//month
which won't find your route.
I think You should make required at least {employee_id} (without question mark) and always passing first parameter.
I suggest to use a get variable.
If you have multiple optional parameters
Route::get('test',array('as'=>'test','uses'=>'HomeController#index'));
And inside your Controller
class HomeController extends BaseController {
public function index()
{
// for example /test/?employee_id=1&month=2
if(Input::has('id'))
echo Input::get('id'); // print 1
if(Input::has('page'))
echo Input::get('page'); // print 2
//...
}
}
Hope this works for you! More information at https://stackoverflow.com/a/23628839/2859139
I am wondering if it is allowed to access/get/use the variable from one function to another function within one controller?
Thank you.
Try Something like this.
class Index extends CI_Controller {
protected $var1;
public function index() {
$this->setVar1();
echo $this->var1; // print $var1
}
public function setVar1() {
$this->var1 = 1;
}
}
$superduper = 'superduper' ;
available only locally within the method (function)
or passed like
$this->reviews->insertToReviews($superduper) ;
versus
$this->superduper = 'superduper' ;
as soon as $this->variablename is declared in a controller its instantly available to:
any method in the controller
any method in any model that is called by the controller
any view file called by the controller
so without passing the variable - in a view you can use
echo 'my lunch is ' . $this->superduper ;
but often its better to explicitly pass values to the view especially if they are unique to the method - it makes it easier to see what is going on. so in that case in controller:
$data['superduper'] = $this->superduper ;
and in view
echo 'my lunch is ' . $superduper ;
Now when anyone looks at the method in the controller - we can see that superduper is being passed to $data. the point is that even though you can avoid passing variable names to methods or the view by declaring $this->somename - if you pass them locally it can make it easier to see what is going on.
The flip side is something like:
$this->error_message = "Error retrieving database records";
is awesome. you can have error messages in any method and they will be automatically available no matter what else happens. so then in your view file have something like
if($this->error_message != '') { echo $this->error_message ;}
this is especially helpful while you are building the site.
I have stored a variable in register by Mage::register('captcha', $var); in helper. And in the controller i tried to retrieve the variable by using Mage::registry('captcha'); But i dont getting any values here. Please help me to solve this.
In your helper file create a function like below :
public function getCaptcha(){
$var = 'myValue123';
Mage::register('varun', $var);
return Mage::registry('varun');
}
In your controller function:
$registryValue = Mage::helper('yourModule')->getCaptcha();
echo registryValue ; //prints myValue123
Hope it helps !!!
It's look like syntax is right.
Please first try to set some static value like $var="test"
Mage::register('captcha', $var);
after that got this value in controller.
Mage::registry('captcha');
if you got this value test then i think you have problem with $var in your helper.
Let me know if you have any problem
'captcha' is already in use, so magento never set your data in registry. Change the name, for example 'captcha1'
Mage::register('captcha1', $var);
This seems really basic but i can't get the hang of it.
I'm trying to send more then one parameter to a method in the controller, like this :
http://localhost/ci/index.php/subjects/3/state
This is the routings i've tried :
$route['subjects/(:num)'] = 'subjects/view/$1';
$route['subjects/(:num)/{:any}'] = 'subjects/view/$1/$2';
the method accepted 2 paremeters :
public function view($slug, $id = null){
}
but i seem to get a 404. How can i get this to work? i need the view method to always accept 1 parameter and optional other parameters.
NOTE : I am including the url helper.
you have problem with your route brackets just change it from {} to () brackets will work
from
$route['subjects/(:num)/{:any}'] = 'subjects/view/$1/$2';
to
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
Always maintain your routing rules
like
$route['subjects/(:num)/(:any)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3/$4';
$route['subjects/(:num)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3';
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
always follow this pattern for routing
if you add like this
$route['subjects/(:num)/(:any)'] = 'subjects/view/$1/$2';
$route['subjects/(:num)/(:any)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3/$4';
$route['subjects/(:num)/(:any)/(:any)'] = 'subjects/view/$1/$2/$3';
then always first condition will be true every time.
also refer this link --> codeigniter routing rules
I once tried this URI pattern
$route['(:any)'] = 'welcome/list1/$1';
$route['(:any)/(:num)'] = 'welcome/list1/$1/$2';
$route['(:any)/(:any)'] = 'welcome/list2/$1/$2';
$route['(:any)/(:any)/(:num)'] = 'welcome/list2/$1/$2/$3';
$route['(:any)/(:any)/(:any)'] = 'welcome/list3/$1/$2/$3';
but it didnt worked ... so I replaced it with regular expression
([a-z 0-9 -]+) replaced (:any)
and
([0-9]+) replaced (:num)
so it became
$route['([a-z 0-9 -]+)'] = 'welcome/list1/$1';
$route['([a-z 0-9 -]+)/([0-9]+)'] = 'welcome/list1/$1/$2';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)'] = 'welcome/list2/$1/$2';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)/([0-9]+)'] = 'welcome/list2/$1/$2/$3';
$route['([a-z 0-9 -]+)/([a-z 0-9 -]+)/([a-z 0-9 -]+)'] = 'welcome/list3/$1/$2/$3';
And it worked for me :)
In order to access the variables in your controllers, you can assign any parameter in the function.
class Welcome extends CI_Controller {
public function list($first, $second)
{
var_dump($first);
var_dump($second);
}
}
I'm writing my own component for Joomla 1.5. I'm trying to figure out how to generate an "alias" (friendly URL slug) for the content I add. In other words, if the title is "The article title", Joomla would use the-article-title by default (you can edit it if you like).
Is there a built-in Joomla function that will do this for me?
Line 123 of libraries/joomla/database/table/content.php implements JFilterOutput::stringURLSafe(). Pass in the string you want to make "alias friendly" and it will return what you need.
If you are trying to generate an alias for your created component it is very simple. Suppose you have click on save or apply button in your created component or suppose you want to make alias through your tile, then use this function:
$ailias=JFilterOutput::stringURLSafe($_POST['title']);
Now you can insert it into database.
It's simple PHP.
Here is the function from Joomla 1.5 source:
Notice, I have commented the two lines out. You can call the function like
$new_alias = stringURLSafe($your_title);
function stringURLSafe($string)
{
//remove any '-' from the string they will be used as concatonater
$str = str_replace('-', ' ', $string);
$str = str_replace('_', ' ', $string);
//$lang =& JFactory::getLanguage();
//$str = $lang->transliterate($str);
// remove any duplicate whitespace, and ensure all characters are alphanumeric
$str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);
// lowercase and trim
$str = trim(strtolower($str));
return $str;
}