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/
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'm new in Yii2 so I have Brands table with their types ('brand', 'author', 'company') and their slug name so I need the URL like that www.site.com/{brand_type}/{brand_slug} without controller name so how to do that ?
This is commonly called pretty URLs. To do achieve that in Yii2 put this in your app config file under 'components' key
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
// ...
'<type:\w+>/slug:\w+>' => 'yourcontroller/youraction',
// ...
],
],
The result is that when you passed a URL in the format you specified, your controller will $type and $slug as parameters you can use in your controller which is expected to take the form:
class YourcontrollerController extends YourBaseController
{
...
public function actionYouraction($type, $slug)
{
// Do whatever you want with these variables
}
...
}
Notice that you will need your web server to configure executing your app's index.php even if it is not in the URL. For Apache this can be done, for example, using .httaccess (More details here) :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
The Definitive Guide to Yii 2.0 has an excellent section about this topic
I've created a module named catalogue with, for the moment, two actions in the default controller:
actionIndex
actionLineProducts
in the index view I have some link which run the line-product's action, the url was the result of:
Url::to(['line-products', 'line' => $line->name])
my goal is to obtain a link like
catalogue/{line-name}
where line-name is the parameter I send to action LineProducts.
My urlManager configurations are:
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<moduls:catalogue>/<controller:default>/<action:line-products>/<line:*>' => '<module>/<line>',
'<controller:\w+>/<id:\d+>' => '<controller>/view/',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
.htaccess configurations:
RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php
Can anyone explain me why my URL is always like:
http://my-site.dev/catalogue/default/line-products?line={line-name}
You swapped the configuration. The key is the pattern of the url as passed from the browser. The value is the route. Since you are using a specific module, controller and action you can pass those in directly:
'rules' => [
'catalogue/<line>' => 'catalogue/default/line-products'
...
]
You can read the Routing and Url Creation page in the Yii2 guide for more information.
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)
I have the following url structures in my Yii2 application
http://127.0.0.1/frontend/web/index.php?r=site%2Flogin
http://127.0.0.1/frontend/web/index.php?r=site%2Fpage2
http://127.0.0.1/frontend/web/index.php?r=site%2Fsample
http://127.0.0.1/frontend/web/index.php?r=site%2Fsignup
How can I convert that URL to something like
http://127.0.0.1/login.php
http://127.0.0.1/page2.php
http://127.0.0.1/sample.php
http://127.0.0.1/signup.php
I should remove frontend/web/index.php?r=site%2F
I tried like and it didn't work
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use http://127.0.0.1/frontend/web/
RewriteCond %{HTTP_HOST} !^http://127\.0\.0\.1/frontend/web/$
RewriteRule ^(.*) http://127.0.0.1/frontend/web/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^login\.php$ /index.php?r=site%2Flogin [L]
I also tried like and it didn't work too.
RewriteEngine on
RewriteRule ^frontend/web/index.php?r=site%2F([^\./]+) /$1.php [L]
No need to change .htaccess to achieve this. Adjust urlManager component.
Add this to your application config:
'components' => [
'urlManager' => [
'enablePrettyUrl' => true, // Cancel passing route in get 'r' paramater
'showScriptName' => false, // Remove index.php from url
'suffix' => '.php', // Add suffix to all routes (globally)
],
// Compare requested urls to routes
'rules' => [
'login' => 'site/login',
'page2' => 'site/page2',
'sample' => 'site/sample',
'signup' => 'site/signup',
],
],
As for removing controller part from all other routes - it violates key MVC concepts.
How you define to which controller requested action belongs in that case?
And what in case of actions with the same name?
For example: http://127.0.0.1/create.php - should it load site/create or users/create?
Also I don't sure if it's good practice, but you can write comparisons to all routes the same way with rules, but all action names should be unique.
Conclusion: you can modify urls to desired view as I mentioned above, but omitting controller names is only recommended for default controller (SiteController).
Official documentation:
UrlManager
$enablePrettyUrl
$showScriptName
$suffix
$rules
Routing and URL Creation