There may be a lot of question regarding this but no help from these all . . I have the same issue as On this link.
I am requesting from url http://example.com/projects/phase/test_phase2#5591409f-ce8c-4a8a-a92d-77360a11ef94 to save the progress and this is requesting on the url http://example.com/phases/update_progress . now i want to redirect my page on back to the url . I am using these all . But no luck .
$this->redirect($this->referer('/'));
$this->redirect($this->referer());
$this->redirect($this->request->referer());
$this->redirect( Router::url( $this->referer(), true ) );
after this it is redirecting to http://example.com/phases/example.com But i want it to be redirect on http://example.com/projects/phase/test_phase2#5591409f-ce8c-4a8a-a92d-77360a11ef94.
routes.php is
Router::parseExtensions('json');
Router::connect('/', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/admin', array('controller' => 'dashboard', 'action' => 'index', 'admin' => true));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
.htaccess is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
it is working very well on localhost . All files are same on both server.
Is there any other method to do this ??
I tried to solve this issue by creating a HistoryComponent that saves the requests in the user's session (including the get parameters). The obvious disadvantage of this approach is that it relies on sessions. Since the session is the same in all tabs the back buttons might influence each other in a multi-tab environment.
If you want to use it:
Add the plugin in bootstrap.php
Use the previous_page variable in your view (pass it to the back button)
Related
I'm new to CakePHP. I have a website that was built by someone else in CakePHP 3.3.16. There are links to website.com/page and also website.com/page/.
What's the best way to get /page/ redirecting to /page without affecting anything else that might start with a /page/?
The route.php has this...
$routes->connect('/page/*', ['controller' => 'Page', 'action' => 'something']);
Then I have a PageController.php which has
public function something($site_id = null)
{
...
}
Will this work in the routes.php? How would I specify that this is a 301 redirect?
use Cake\Routing\RouteBuilder;
Router::scope('/', function (RouteBuilder $routes) {
$routes->redirect('/page/','http://website.com/page');
$routes->connect('/page/?*', ['controller' => 'Page', 'action' => 'something']);
});
This doesn't seem to work in the .htaccess (/page/ is displayed and not redirected)...
Redirect 301 /page/ http://website.com/page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
as a first glimpse to fix this would be instead of putting the Route::scope would be Router::connect and Router::redirect imo.
Therefore, an approach to a solution would be first doing something like this.
Router::connect('/page/*', ['controller' => 'Page', 'action' => 'something']);
And then you redirect the page with the cake command redirect:
Router::redirect('/page/','/page', array('status' => 301));
In the project that I use which is CakePhp 2.6, I always have redirected pages like this depending on the task. Sometimes you can do this type of redirects inside the Controller but it is best to avoid it for not mixing routing with programming logic.
I am new in cakephp trying to loading default controller as Pages
This is my route :
Router::redirect ('/', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/pages/**', array('controller' => 'pages', 'action' => 'display'));
When I run "http://localhost/project/index.php" then its working fine but try with "http://localhost/project/" its not loading default controller (Pages)
Without htaccess & with htaccess its giving same issue.
This is error:
Controller class ProjectController could not be found.
Error:
The requested address '/project/index.php/project/' was not found on
this server.
You are running in subdirectory, so you should set RewriteBase:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /project/index.php?url=$1 [QSA,L]
</IfModule
As per cakephp manual :
http://book.cakephp.org/3.0/en/development/routing.html#redirect-routing
You should try
Router::scope('/', function ($routes) {
$routes->redirect(
'/home/*',
['controller' => 'Articles', 'action' => 'view'],
['persist' => true]
// Or ['persist'=>['id']] for default routing where the
// view action expects $id as an argument.
);
})
Instead of Router::redirect.You should try this method once, might issue got resolved.
Its resolved
Added baseUrl in app controller:
function beforeRender(){
$this->set('baseUrl', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));
}
Removed App.baseUrl from Core.php :
Configure::write('App.baseUrl', env('SCRIPT_NAME'));
now its working fine on:
http://localhost/app/
So I started making the admin panel of our site as a plugin. And I'm able to redirect every request like domain.com/admin or domain.com/admin/users or domain.com/admin/pages/edit/5 - to the appropriate controller and action from the admin plugin.
Like this one:
Router::connect('/admin', array('plugin' => 'admin', 'controller' => 'index', 'action' => 'index'));
Router::connect('/admin/users/list', array('plugin' => 'admin', 'controller' => 'users', 'action' => 'list'));
But this means I will have to write separate route for almost each URL ??? or actually - for each action ... So - is there a way to set it globally? ...
For example:
Router::connect('/admin/users/*', array('plugin' => 'admin', 'controller' => 'users'));
Or even better:
Router::connect('/admin/*', array('plugin' => 'admin'));
Cause the last two examples didn't work at all ...
EDIT: the CakePHP version is the latest at the moment - 2.4.1.
Normally you shouldn't have to create such routes for plugins, the basic mapping works out of the box, ie in case your plugin is named admin, then /admin/users/list will automatically map to plugin admin, controller users, action list.
An exception is your /admin route, by default this would look for index on AdminController. So for mapping to index on IndexController you'll need a custom route like the one in your question:
Router::connect('/admin', array('plugin' => 'admin', 'controller' => 'index', 'action' => 'index'));
Apart from this it should work as is in case you don't have any other preceding rules that will override the default behaviour, something like Router::connect('/*', ...) for example. In case there are such rules and you cannot change them, then this is how you could connect the basic routes of your plugin:
Router::connect('/admin/:controller/:action/*', array('plugin' => 'admin'));
Router::connect('/admin/:controller/*', array('plugin' => 'admin', 'action' => 'index'));
Router::connect('/admin/*', array('plugin' => 'admin', 'controller' => 'index', 'action' => 'index'));
Note that this needs to be placed before the other routes that override the default behaviour!
See also http://book.cakephp.org/2.0/en/development/routing.html
On a side note, list as an action name will probably not work, this should throw a parser error as list is a reserved keyword. So you in case this isn't just an example you'll need an extra route for that if you want to use /list as the action name in the URL.
I'am using CakePHP 2.0 for a small admin section of a website.
If the user is logged in correctly, I redirect them to the admins dashboard.
I do this as following:
$this->redirect(Router::url(array('controller' => 'admin', 'action' => 'dashboard')));
The redirect is done correct, but for some reason the URL where it redirects to isn't correctly build. The URL is in the following structure (note the double [root] sections in the URL - this is what is wrong):
http://localhost/[root]/[root]/admin/dashboard
Off course their are errors shown because this controller / action doesn't exists. The URL should be off this form:
http://localhost/[root]/admin/dashboard
I can't seem to find what the exact problem is since cakePHP is not my core dessert, is there anyone that can point me in the right direction?
Thanks!
$this->redirect("YOUR URL");
example $this->redirect('/admins/dashboard');
This way you can redirect easily!
You can use the 'full_base' parameter in Router::url.
e.g.
$url = Router::url( array(
'controller' => 'posts',
'action' => 'view',
'id' => $post[id],
'full' => true
) );
This will give you a full base url. I found this fixed routing problems when I was running CakePHP in a subdirectory of localhost.
you can simply right like this
$this->redirect(array('controller' => 'admins', 'action' => 'dashboard'));
You can simply right this
Router::connect('/', array('controller' => 'users', 'action' => 'index'));
I added CakeDC-Users plugin in app/plugins.
Now for http://example.com/ , if i click the home/index link, it wrongly redirects to http://example.com/users/posts/index , but it should be http://example.com/posts/index .
Why is the plugin 'users' always added before the respective controller?
If i delete the CakeDC-Users plugin from app/Plugin and delete that line CakePlugin::loadAll(); from bootstrap.php then i get normal link/route
routes.php:
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
How can i fix that problem addin CakeDC-Users plugin
That is the way how plugins are accessed. You can define your custom route this way
Router::connect('/posts', array('controller' => 'posts', 'plugin' => 'users'));
This will be done in app/Config/routes.php
if it is for link issues you will have to explicitly specify
echo $this->Html->link('link', array(
'controller' => '',
'action' => '',
'plugin' => false)
);
But i would prefer custom routing.
For more info, you can always sneak into the CookBook