How do you add in the yii form the action for PHP_SELF, I am currently using this:
'action' => '/site/contact',
but I want to replace the action to call itself again. How do I do that?
UPDATE:
tried removing the action in the array, now it does not go into validating the form.
Use Yii controller method createUrl - calling it without params will point to current controller and action.
In views you can just call $this->createUrl():
'action' => $this->createUrl(),
Important: do not hardcode actions like in your question. Always call createUrl, this will ensure proper urls if you change url rules:
'action' => $this->createUrl('otherController/someAction'),
or same controller different action
'action' => $this->createUrl('otherAction'),
etc.
Do mean perhaps a page refresh?
Just do $this->refresh() inside your controller action.
Related
In a Zend application with an example url like this one:
http://example.com/profile/423423(some id)
How do I get param from url?
I try to use:
$this->getRequest()->getParam('action');
But I get action does not exist.
then I try something like this:
protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('profile/:catid', new Zend_Controller_Router_Route('profile/:catid',
array(
'module' => 'default',
'controller' => 'profile',
'action' => 'index' // Check your action and controller
)));
}
How do I get param from url?.
You can use the getParam() methods to retrieve request variables like route, GET and POST.
Inside your controllers action you can do this.
$this->getParam('parameterName'); or $this->getRequest()->getParam('parameterName');
If you want to get the name of action and controller that matched the current route you need to use following methods.
$this->getRequest()->getActionName() and $this->getRequest()->getControllerName()
You should just use
$this->getRequest()->getParam('profile');
from your controller.
You shouldn't need to use a custom route.
Kind regards
Garry
EDIT
I have just noticed that you wish to use the profile controller. The simplest solution would be to add an id into your url like.
http://example.com/profile/id/423423
and get the profile id with
$this->getRequest()->getParam('id');
i am having following url
example.com/show.php?xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I want to redirect it to the controller tests action redirs.
How can i achieve this with cake php routing or via htaccess.
You can just enter a plain route for this in the app/Config/routes.php file:
Router::connect('/show.php', array('controller' => 'tests', 'action' => 'redirs'));
Within your action you can process whatever is passed in the querystring, if there is a need for that.
Im using "Zend form". What should I do to get correct "action" parameter in my FORM tag?
For example, the form Form_Search used in SearchController in indexAction. Where and how should I set the action parameter to form to make it "/my/app/directory/search/index"?
Update: "action" parameter should be the same as the form will assume if not set. I just need the param in FORM to simplify JS coding.
You can use setAction method of Zend_Form, e.g.:
$yourForm->setAction('/my/app/directory/search/index');
Hope this is what you are looking for.
EDIT:
You can retrieve many info about the request, in an action, using $this->getRequest() method that returns an instance of Zend_Controller_Request_Http. For example, if to get basePath and everything between the BaseUrl and QueryString you can do:
$infoPath = $this->getRequest()->getPathInfo();
$basePath = $this->getRequest()->getBaseUrl();
$yourForm->setAction($basePath . $infoPath);
Zend_Controller_Request_Http has other methods that might be useful to you.
You can use the url() view helper to create your action urls.
$view->url(array(
'module' => 'mymodule',
'controller' => 'mycontroller',
'action' => 'myaction',
'param1' -> 'myvalue1',
// etc
), 'default');
// 'default' here refers to the default route.
// If you specify a custom route, then that route will be used to
// assemble() the url.
Depending where you call $form->setAction(), you can access to the $view in different ways.
In the controller: $this->view
In the form itself: $this->getView()
In a view script: $this
In view helper extending Zend_View_Helper_Abstract: $this->view
Hope it helps!
Just an addition to previous answers. Kind of "industry standard approach" is to render/validate/process the form all on the same page. Then there is no need to set the action as it's filled with current URL.
Exception is for example serch field in layout. Then use $form->setAction($url). But always remember to echo the form in the search action. If the form value is invalid, ZF would expect you to render it in page with the errors. Or you can use $form->getErrors().
`$this->setAttribs(array(
'accept-charset' => 'utf-8',
'enctype' => Zend_Form::ENCTYPE_URLENCODED,
'method' => 'get',
'action' => '/controller/action/param/attr'
))`
Im developing a simple application with CakePHP v1.3.7 Stable. I want to generate a simple user profile page, accessible by the url: my.domain/u/id, where id is the id of the user in database.
So i wrote this (and only this) in app/config/routes.php:
Router::connect('/u/:id',
array('controller' => 'Users', 'action' => 'profile')
,array('pass'=>array('id'),'id'=>'[0-9]+')
);
The above code works fine, when i put my.domain/u/120 in the browser, it shows the profile of user 120.
But, when i try to create a link to this page using the Html helper:
// some code in a view
$this->html->link('Test', array('controller'=>'Users', 'action'=>'profile', 120))
The html helper (doing inverse routing, i think) generates the url in the defaut cakephp form: Test
Based on the configuration in routes.php, it should be: Test, right?
I'm missing something?
Thanks.
Try with this:
$this->html->link('Test', array('controller'=>'Users', 'action'=>'profile', 'id'=>120))
I hope the missed 'id' will fix it.
Finally, thanks to dogmatic69, I deleted the id related code of the route.
I end with this in routes.php:
Router::connect('/u/*',
array('controller' => 'users', 'action' => 'profile')
);
Now, the html helper works just as expected:
echo $this->html->link('Test',array(
'controller'=>'users',
'action'=>'profile',
100
))
// renders: Test
The drawback is that I can't take advantage of the regexp filter of the router, as in the initial route code.
I have a set of rather static pages wich I moved to the views/pages folder. The resulting *.ctp files are editable by my customer through CushyCMS (simplistic cms perfect for dummy proof editing). However CushyCMS generated preview links that obviously don't take CakePHP into account. I would like to solve this little problem with custom routing, but can't get my head around the details..
How can I dynamically connect the url http://localhost:8888/cake125/app/views/pages/test.ctp to http://localhost:8888/cake125/pages/test?
I added the following in my routes.php:
Router::connect('/pages/test.ctp', array(
'controller' => 'pages',
'action' => 'display', 'test'));
This works ok for connecting: http://localhost:8888/cake125/pages/test.ctp to http://localhost:8888/cake125/pages/test. Somehow following snibbet doesn't do the trick:
Router::connect('/app/views/pages/test.ctp', array(
'controller' => 'pages',
'action' => 'display', 'test'));
Ideally I'd like to have a single Router::connect statement which connects all /app/views/pages/*.ctp requests to the right place.
Finally I would also like to correctly handle google search results for the old version of the site. Like so:
Router::connect('/test.html', array(
'controller' => 'pages',
'action' => 'display', 'test'));
This works ok but I'd rather have anypage.html connect to /pages/anypage. Can anyone help with this?
Thanks in advance!
First, by virtue of having Cake in a subdirectory (/cake125), I think you may need to connect the /cake125/:controller/:action, rather than how you have it. Not 100%, though; Cake might be robust enough to handle that use case. If you have weird errors, I'd check that.
On with my answer:
I think you are somewhat misunderstanding how the Router class works. You connect URLs, not relative filesystem paths, using Router::connect. By default (which you may have erased, but it's pretty simple to fix), Cake will route requests to /pages/* to the PagesController::display() function, passing it one argument (the action listed in the http request).
So, to have the pages controller map /pages/one to the app/views/pages/one.ctp element, simply make sure that the following (default, i.e. Cake normally has this setup) line is in the routes config (and make sure that lines above it do not match that pattern):
Router::connect( '/pages/:action', array( 'controller' => 'pages', 'action' => 'display', :action);
This should ensure that PagesController::display( $action ) is invoked during the request, which is (I think) what you're after.
If your CMS generates preview links that you want to correctly re-route, I'd suggest adding a new route. E.g., if your CMS generates links like http://somesite.com/cms/preview/newly_edited_file, you can route it like this:
Router::connect( '/cms/preview/:action', array( 'controller' => 'pages', 'action' => 'display', :action );
For your second question: have a default rule in your routes (make it the last rule, and have it match *). It will then be configured to route all not found requests to your controller/action pair as requested. Try this:
Router::connect( '/:action', array( 'controller' => 'pages', 'action' => 'display', :action );
Major caveat this will break your existing routes. You will need to manually add an entry for each of your existing controllers (Router::connect( '/users/:action', ...etc...). If you google around you can find some clever solutions, such as having that list generated at runtime for you. But you will need to address "normal" routing, once you've added that catch-all (and make sure your catch-all is at the end of the routing file).
Also, if you want to parse URLs like /test.html, simply add a call to Router::parseExtensions(...) so that Cake will register .html as an extension for it to parse. Check the manual on that function for more info.
As others have pointed out how CakePHP Router works, I'll leave it at that.
For the second part of your question (handling old links), I'd suggest adding this to the end of your Routes list:
Router::connect( '/:page',
array (
'controller' => 'pages',
'action' => 'display',
),
array (
'pass' => array ('page'), // to pass the page as first arg to action
'page' => '.+\.html$', // to verify that it ends with .html
)
);
You'd unfortunately have to parse out the .html yourself though
How can I dynamically connect the url http://localhost:8888/cake125/app/views/pages/test.ctp to http://localhost:8888/cake125/pages/test?
Well, the thing is, you don't. :-)
What I mean by that is, you do not connect a URL to another URL. What you really do is, you make certain URLs trigger certain Controller functions (or Actions for short) which in turn may (or may not) render certain Views. By default it's all straight forward through naming conventions. The URL /foo/bar triggers the Controller Foo's Action bar and renders the View /views/foo/bar.ctp.
The PagesController is already a special case. The URL /pages/foo triggers the Controller Pages's Action display, passes it the parameter foo, which renders the View /views/pages/foo.ctp. Notice the difference in which Action is triggered.
Since there are a lot of steps inbetween, it's not a given that a certain URL corresponds to a particular file on the hard disk. The URL /foo/bar might trigger Controller Baz' Action doh which renders the View /views/narf/glob.ctp.
This makes translating http://localhost:8888/cake125/app/views/pages/test.ctp to render the file /views/pages/test.ctp somewhere between an uncertainty and a pain in the rear.
Edit:
Having said that, the particular problem in your case is that the base URL is http://localhost:8888/cake125/app/. You can invoke a Cake app from http://localhost:8888/cake125/, http://localhost:8888/cake125/app/ or http://localhost:8888/cake125/app/webroot. All three URLs will be handled by the same file cake125/app/webroot/index.php, if you use one of the shorter URLs the request will be "forwarded" (rewritten) via .htaccess rules.
So the Route you're trying to connect, the Route that Cake sees, is actually /views/pages/test.ctp.
Actually, my mistake, this might not be the problem, but it depends on your .htaccess files and server configuration.
It doesn't seem to make much sense in a CMS though, since every newly created page would need its own rule. So I'd recommend against trying to do so and rather hack Cushy to properly construct URLs using the Cake HtmlHelper or Router::url(). Failing that, connect all URLs with a catch-all rule to some Action, parse the URL there and render the correct View "manually".
Alternatively, use .htaccess files and rewrite rules to actually rewrite the URL into a normal Cake URL, so Cake doesn't have to worry about it. As said above though, this can be very fragile.