Yii url rules for main page - yii

'urlManager'=>array(
'class'=>'application.components.UrlManager',
'urlSuffix'=>'/',
'baseUrl'=>'',
'showScriptName'=>false,
'urlFormat'=>'path',
'rules'=>array(
'<language:\w{2}>' => 'page/index',
'' => 'page/index',
'<language:\w{2}>/page/<alias:.*>' => 'pages/read',
)
link "/en/page/index" works fine
links "/" and "/en" returns the error "Unable to resolve the request" page / index ".
what is wrong with the rules
'<language:\w{2}>' => 'page/index'
'' => 'page/index',
?
UPD:
pagesController has an action:
public function actionRead($alias){
//some php code...
if($model==null)
{
throw new CHttpException(404,'page not found...');
}else
{
$this->render('read',array('model'=>(object)$model));
}
}

Your rules that don't work are redirecting to page/index, meaning that they're going to try and access PageController.php, and within that controller, they're going to try an access actionIndex. It doesn't sound like you have either a controller PageController.php, much less an actionIndex within that controller.
You need to fix the targets of those rules to include valid controller/action combinations.

Related

How to change yii2 default module

Hello I am not an expert in Yii2 and would appreciate any help, We want to change our default module,
Our logic:
site implements a use of wildcard domain, https://example.com,
we implement a bootstrap component to Identify a use of a "subdomain" in the
url I.E. https://sub.example.com,
$config = [
'id' => 'basic',
'name' => 'exapmle',
'basePath' => dirname(__DIR__),
'bootstrap' => [
'log',
'devlogin',
'app\components\SubBootstrap', #this is the bootstrap component we use
'app\components\ThemeBootstrap',
],...
now we would have liked to use the same logic to change the default module to a new "submodule" but we can't use the bootstrap because it happens after the default module has been applied.
obviously we can have an explicit url call for the module I.E.
'modules' => [
'sub'=>[
'class' => 'app\modules\sub\Module',
],...
but that means that the url would look like https://somesub.example.com/sub/ which is undesirable.
thank you very much
In your case, what you can do is override the UrlManager component and manually adjust the path to reflect the module that you want to envoke behind the scenes.
So your code would look something like this:
<?php
namespace app\components;
use Yii;
class UrlManager extends \yii\web\UrlManager
{
public function parseRequest($request)
{
if (!empty(Yii::$app->sub)) {
$pathInfo = $request->pathInfo;
$moduleIds = array_keys(Yii::$app->modules);
$inModule = false;
foreach ($moduleIds as $moduleId) {
if (preg_match("/^{$moduleId}/", $pathInfo)) {
$inModule = true;
break;
}
}
if (!$inModule) {
$pathInfo = 'sub/' . $pathInfo;
$request->setPathInfo($pathInfo);
}
}
return parent::parseRequest($request);
}
}
and then in config/web.php:
'urlManager' => [
'class' => 'app\components\UrlManager',
...
],
You don't need to change the module configuration. You need to change web-server path to this module and architech the UrlManager rules;
https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing
Bootstraping yii module it's only some way to load it before another components.
https://www.yiiframework.com/doc/guide/2.0/en/runtime-bootstrapping

a route doesnt work (yii)

Hy stackoverflow !
I'm trying to make a form into an external page with Yii 1.1.14 (this is an old site).
I've made a directory into my views call signinPartners and into this one, a php file call signin.
I have also created a controller :
class SigninPartnerController extends Controller
{
public function actionSignin(){
$this->render('/signin');
}
}
He renders the route /signin defined in my config by :
return array(
'' => 'index',
'signin' => 'signinPartners/signin',
);
but when I try the URL http://mylocalserver/signin the site send back Error 404 Unable to resolve the request « signin-partners/signin »..
This is really disturbing because there are other URL's on my website and they work the same way without throwing an error 404. I don't know what I missed... Can somebody help ?
my urlManager :
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false, // do not display index.php in the url
'urlSuffix' => '/',
'rules' => $routesRules, //this variable contains the array defined above
),
I have also check my runtime :
2018/01/17 11:12:16 [error] [exception.CHttpException.404] exception 'CHttpException' with message 'Impossible de résoudre la requête « signinPartners/signin ».' in D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\web\CWebApplication.php:286
Stack trace:
#0 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\web\CWebApplication.php(141): CWebApplication->runController('signinPartners...')
#1 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\library\Yii\base\CApplication.php(183): CWebApplication->processRequest()
#2 D:\Windows\Windows\CommonFiles\wamp64\www\MoovTime-Conso\public\frontend\index.php(36): CApplication->run()
#3 {main}
REQUEST_URI=/signin
---
(the exception message is in french and means Unable to resolve the request « signin-partners/signin »)
Ok, big update, i've tried to play with routes.php and I realized that the name of my controller doesn't match with signinPartners. So, I update the routes rules with :
return array(
'' => 'index',
'signin' => 'signinPartner/signin',
);
And now, we have a new error : Controller can't find the view « /signin »..
There is the post Controller can't find the view in Yii which can anwser this question !
ANSWERED
try adding a - in signinPartners.
'signin-partners/signin'
The problem in my case was the action, the directory name in my view that contains the page and the route rule.
first, I update the route rule from :
return array(
'' => 'index',
'signin' => 'signinPartners/signin',
);
to :
return array(
'' => 'index',
'signin' => 'signinPartner/signin', // controlerName/actionName
);
The controller name was wrong.
In a second time, to match with the name of the controller, I simply renamed the directory in views calls signinPartners to signinPartner (only for readability).
Finally I update the action actionSignin that was :
public function actionSignin(){
$this->render('/signin');
}
For :
public function actionSignin(){
$this->render('/signinPartner/signin');
}
Where I change the path with the new directory name.

Routing in module doesn't work Yii 2

I am new in Yii 2 and my problem is about routing inside a module.
I have a module in my app which is a profile cabinet both for users and admins. I created a CabinetController instead of DefaultController and also I created a AdminController and UserController.
What I want? I want this CabinetController received request and forward it to either AdminController or UserController after verify wether the user is admin or not.
In config file I set a default route for module as "cabinet"(as I understand this is a name for default controller). And in "rules" part of UrlManager I wrote following:
'modules' => [
'cabinet' => [
'class' => 'app\modules\cabinet\Module',
'defaultRoute' => 'cabinet'
],
'utility' => [
'class' => 'c006\utility\migration\Module',
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<_c:\w+>/' => '<_c>/',
'<_c:[\w\-]+>/<_a:\w+>' => '<_c>/<_a>',
'<_m:cabinet>/<_a:\w+>' => '<_a>',
],
],
If I go to "my-site.com/cabinet" it works fine and open "admin/index" because I made it to redirest this request to AdminController/actionIndex, but once I go to somewhere like "my-site.com/cabinet/users" it respond with 404 NotFound. I open the loger and see: exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "cabinet/desktop"
This is my CabinetController and the way I forward requests to Admin/UserController:
public function init()
{
parent::init();
$this->user = $this->findModel();
$this->controllerToUse = $this->user->isAdmin() ? 'admin' : 'user';
}
public function actionIndex()
{
return $this->module->runAction($this->controllerToUse . '/' . $this->action->id);
}
If I change defaultAction in CabinetController it run this action normally as expected. Or if I go to "my-site.com/cabinet/admin/users" again it works good, because it found a controller in the url(as I think).
Routing can be a bit tricky in Yii2, it follows a few rules you need to understand which can be found here
But if i understand you correctly Admin/UserController is part of the Cabinet module? and you want Yii to route /cabinet/users to /cabinet/admin/users
You'll need to add some rules in your UrlManager see Rules
Example:
'rules' => [
'<module:cabinet>/<action:\w+>' => '<module>/admin/<action>',
],
Hope it helps

Redirecting to 404 route in PhalconPHP results in Blank Page

I have setup a router, and in it, defined a route for 404s:
<?php
use Phalcon\Mvc\Router;
$router = new Router(FALSE);
$router->removeExtraSlashes(true);
$route = $router->add('/', ['controller' => 'index', 'action' => 'index']);
$route->setName("index");
// other routes defined here...
$router->notFound([
"controller" => "index",
"action" => "route404"
]);
?>
My IndexController:
<?php
class IndexController extends ControllerBase
{
public function indexAction()
{
// code removd for berevity
}
public function route404Action() {
// no code here, I just need to show the view.
}
}
?>
And I have a view # /app/views/index/route404.phtml that just has a bit of HTML in it, I even tried making it a .volt file, no luck.
When I go to a page that doesn't match any routes, it works fine. But, if I try to redirect to it, I just get a blank page. For example, in one of my controllers I have this:
if (!$category) {
// show 404
//Tried this next line to test, and it indeed does what you'd expect, I see "Not Found".
// echo "Not Found"; exit;
$response = new \Phalcon\Http\Response();
$response->redirect([
"for" => "index",
"controller" => "index",
"action" => "route404"]
);
return; // i return here so it won't run the code after this if statement.
}
Any ideas? The page is completely blank (nothing in source) and there are no errors in my apache logs.
Try returning the response object, not just a blank return. Example:
return $this->response->redirect(...);
However I would recommend to use Forward from dispatcher to show 404 pages. This way user will stay on same url and browser will receive the correct status code (404). Also it's SEO friendly this way :)
Example:
if ($somethingFailed) {
return $this->dispatcher->forward(['controller' => 'index', 'action' => 'error404']);
}
// Controller method
function error404Action()
{
$this->response->setStatusCode(404, "Not Found");
$this->view->pick(['_layouts/error-404']);
$this->response->send();
}

can't display error page layouts in yii

I am able to render my specified error pages in my modules, but I can't get error layouts outside of my modules.
It's quite strange, in my main config I have:
'errorHandler'=>array(
'errorAction'=> '//site/error',
),
I tried to see if it is pointed to the right file by changing it to //site/contact, and see if it renders the contact page if error. And it does show.
So the path is correct, then how come it's not showing the error page? It shows the default white page with exception error. The exception page is what im expecting, but how it's formatted is not.
public function actionError()
{
$this->layout = '/layouts/main';
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', array('error'=>$error));
}
}
Your user role doesn't have rights to access actionError() in controller.
In your site controller, You can see a function called public function accessRules().
Check error is added for some user role, or it's for admin only.
If error not found, then add it for user roles.
Example:
public function accessRules()
{
return array(
array('allow',
'actions' => array('index', 'view', 'error'),
'users' => array('#')
),
..
..
..
}
You need to create a errorXXX in views/system. The XXX stand for the error code, in this case most likely a 403. For more information see this page on the Yii site.