I'm total newbie on Yii. Professor basically asks us to make school project having shown us three things to do in Yii.
Let's observe two classes I have, their models being: StudProg and NivoStudija.
What I want is to pass attribute 'naziv' from nivoStudija/admin to studProg/admin, because when I click on a particular item nivoStudija/admin, studProg/admin is shown and I want to use this variable there. So I pass argument like this in one of my CGridView widget's items:
CHtml::link($data->naziv, array("studProg/admin", "nivo_naziv" => $data->naziv))
It opens up studProg/admin and I see URL like this:
http://localhost/pmf/index.php?r=studProg/admin&nivo_naziv=Osnovne+studije
My problem is: How do I get this nivo_naziv thing to use it in studProg/admin ?
Thanks in advance.
For Yii1 you need the equivalent code
$my_nivo_naziv = Yii::app()->request->getQuery('nivo_naziv);
I'm assuming that you are using Yii2.
Then you can get the URL parameter with:
Yii::$app->getRequest()->getQueryParam('nivo_naziv');
Try this for Yii1:
Yii::app()->getRequest()->getParam('nivo_naziv');
In Yii if you want to access get and post parameter, you can use getParam function like this.
Yii::app()->request->getParam('nivo_naziv);
http://www.yiiframework.com/doc/api/1.1/CHttpRequest
Related
I need router management in my project.
My url is some thing like it.
http://localhost/cloud/index.php/dashboard/view_tickets/wsyZCMuIEavPeWdRHqjJ
Here i want to replace "dashboard/view_tickets" as one word "tickets"
but i have tried using routers, its not working.
Router code:
$route['ticket/(:num)'] = 'Ticket/view_tickets/$1';
Here $1 is for num, but here my parameter has string only.
Any suggestion?
Thank you.
Change this:
$route['ticket/(:num)'] = 'Ticket/view_tickets/$1';
To this:
$route['ticket/(:any)'] = 'Ticket/view_tickets/$1';
To make it work, you should create a function called "view_tickets" under "Ticket" class (Controller).
Also please see here to understand routing in Codeigniter.
Hi guys I am trying to achieve something that I hope is possible but couldn't find the right way to find.
I am using codeigniter 2.2.0
I want to use an url in codeigniter like
domain/username/controller/method/$args
Let me explain When user types an url like
domain/mike/job/editJob/12/urgent/
Here "mike" is someone's user name
"job" will be a controller alias
"editJob" will be a method
"12" and "urgent" will be parameter of editJob method.
editJob method will have three parameters.
I want "mike" as 1st parameter,
then "12" and "urgent" as second and third parameter.
So far what I have done in my routes
$route['(:any)/job/(:any)'] = 'job_c/$2/$1';
When I type in the url
domain/mike/job/editJob/12/urgent
in Job controller I get
"12" as first parameter
"urgent" as second parameter
and "mike" as third parameter
**
Is there any possible way to get "mike" as first parameter and then the rest is okay**
Edited:
One more thing if I pass three parameters after method then I am not
getting the username!!
I need to have the username as first parameter because there may have multiple parameters in any method and there is a possibility to have conditional parameters as well.
One more thing I want to know. Is it possible to make such route that will work the same as my given route but the controller alias will also be wildcard. Or if there is any way to check if a segment in url is a controller then one route and if not a controller then something else should happen.
I am not a well describer still tried to keep it simple. If anyone knows something that will help me a lot.
Thanks
UPDATE
Is there any way to keep the username "mike" in session from routes.php file thus I don't have to pass this as a parameter.
Updating Again
I have solved the issue somehow.
I am using
$route['(:any)/garage/([^/]*)/([^/]*)/(.*)'] = '$2/$3/$1/$4';
Here garage is nothing but a simple identifier for the route. This route will work when it gets garage as a second segment in url. It will work like
domain/user/garage/anyControler/anyMethod/manyParameters
It's completely dynamic only the garage is used as identifier. If you want you can use your controller name instead of using any identifier. then you don't have to declare same thing multiple times for multiple controllers but it will work fine. It will work for any controller and any method. In addition it supports dynamic number of parameters.
I don't know if there is any better way to do this but it solves my issue. If anyone know something better then this then please share.
I think what is happening is that you are accessing
domain/mike/job/editJob/12/urgent
and its being parsed like:
job_c/editJob/12/urgent/mike
Because:
$route['(:any)/job/(:any)'] = 'job_c/$2/$1';
$2 = (:any)/job/(:any) = editJob/12/urgent
$1 = (:any)/job/(:any) = mike
Substituting:
job_c/editJob/12/urgent/mike
You could try to keep your route as similar as your current one:
$route['(:any)/job/(:any)/(:any)'] = 'job_c/$2/$1/$3';
This will allow you to match $2 to any method name, and have $1 as the first parameter and $3 as the rest of params.
But, I would suggest, if this is a very specific route, substituting the $2 :any, with the actual method name and the expected type of the params, otherwise, you might receive unexpected values to every method in the matching controller.
I would use something like:
$route['(:any)/job/editJob/(:num)/(:any)'] = 'job_c/editJob/$1/$2/$3';
Hope this helps.
Update:
For the controller matching: Code Igniter uses the form controller/method/param1/param2/...
As long as you create routes that matches controllers, methods and params in that order, you can do anything.
$route['(:any)/(:any)/(:any)'] = '$1/$2/$3';
or just
$route['(:any)'] = '$1';
and hopefully it will contain a controller and method and the required params.
I think this will totally be missing the point of having a routing system.
I'm having problem with $app->urlFor().
I have blog named route. Everytime I use $app->urlFor('blog') to create a link, the function is always returning localhost:8000/index.php/blog instead of localhost:8000/blog.
Can someone help me ?
You'll need to configure your webserver to use pretty URLs:
http://docs.slimframework.com/#Route-URL-Rewriting
I am using the Zend framework and what I need is to construct a url in my view. Normally in regular php code I'd just grab the GET Variable by using the global $_GET. However with Zend I'm setting it to clean URIs so :
?ac=list&filter=works&page=2
Looks like
index/ac/list/filter/works/page/2
In my view I'm setting a links cs such that if the GET variable filter equals works then the color of that link would be different and it would point to the same page only linked as so:
index/ac/list/filter/extra/page/2
And like wise I have a number of other links all which just one GET value - how do I set this up - I am using the Zend framework.
To access a request variable direct in the view you could do:
Zend_Controller_Front::getInstance()->getRequest()->getParam('key');
But as others have said, this is not a good idea. It may be easier, but consider other options:
set the view variable in the controller
write a view helper that pulls the variable from the request object
If you need to access a GET parameter from a view, i think you're doing it the wrong way.
I suggest that you set up a route with all your parameters, and then use $this->url from your view to render a valid and correct url.
Fore som more info, check out the following blog post (no, i'm not the author):
http://naneau.nl/2007/07/08/use-the-url-view-helper-please/
Edit:
If you want to be 'lazy', you can set a view parameter from your controller by doing $this->view->param = $this->_getParam('param'). You can then access param from your view by doing echo $this->param;. However, i do not recommend this.
To access the Request Object one way that is common is to save it in the Registry.
http://osdir.com/ml/php.zend.framework.mvc/2007-08/msg00158.html
http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-access-request-object-customizing-layout-view-3349.html
You can pass it in from a controller: $this->view->page = $this->_getParam('page');.
Footnote: I agree with #alexn.
i am using Zend Framework v1.11 and i am doing like this
In Controller
$this->view->request = $this->_request;
then in View you can access any Request param like this
<h3><?= $this->request->fullname ?></h3>
When I use CakePHP Paging I get an url like this:
http://example.com/php/page:2
What do I have to change in the controller, the view and the routes.php to create a working url like this:
http://example.com/php/2
Oh yes, now I see your question. Well you could do something like:
function index($page){
$this->paginate = array('page'=>$page);
$this->set('stuff', $this->paginate('YourControllerName'));
}
See here for more details:
http://bakery.cakephp.org/articles/view/basic-pagination-overview-3
Also, of course you should do some validation that the page is an actual number and that the page would even exist but that is the basics of it i think.
About the routes and views, I have never tried but have a look at these posts on the cake groups, I think they have a problem similar to yours.
http://www.mail-archive.com/cake-php#googlegroups.com/msg45878.html
Try this link:
http://www.sakic.net/blog/changing-cakephp-pagination-urls/
My guess is that this won't be easy to automate, you'll definitely need to do some tweaking.
For starters, you'll probably have to create your own paginator helper and inherit the default one. By the looks of the code, you'll need to override the link-generating code in PaginatorHelper::__pagingLink(), but probably numbers() and prev() etc.. since they all create links with the page param.
Maybe a better way would be to override your AppHelper::url(), check for the "page" param there and modify the url to accomodate your needs.
But, I haven't tried all this, so no guarantees..