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.
Related
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
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();
}
I have many modules and depending upon modules routes are generated dynamically. For explaining purpose I m adding route for just one module. Following are the routes for the users module.
<?php
//1
$router->add('/users',array(
'module' =>'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index'
));
//2
$router->add('/users/:params/',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action' => 'index',
'params'=>1
));
//3
$router->add('/users/:action',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1
));
//4
$router->add('/users/:action/:params',array(
'module' => 'users',
'namespace'=>'Backend\users\Controllers\\',
'controller'=>'index',
'action'=>1,
'params'=>2
));
?>
Lets say the url for the user module is
http://www.example.com/admin/users/
This url matches the very first route and its working as expected. But when we navigate to next page, my url looks like
http://www.example.com/admin/users/2
now the problem is it should match 2nd route, but it matches 4th route. If I move 2nd route all the way down, the above url work, but the url
http://www.example.com/admin/users/search/1 will not work
Can anybody help me make it work?
Thanks
:params is tricky because it will match anything. So /users/:params/ matches both /users/:action and /users/:action/:params, which itself has :params in it - making it a routing mind blow.
As a general rule avoid the mind blow scenario. For example you can put :params at the end of the longest possible match (/users/:action/:params) and then rewrite the shorter routes without any :params in them.
'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.
I need to use a file manager in pop up which comes on a button click. I am using Yii extension elfinder. I am finding it hard to understand the way of using it. I downloaded the code from bitbucket, put it inside my application in the folder extension. I try to test it using new controller, named it elfcontroller and put the following code (got from the website)
class ElfinderController extends CController
{
public function actions()
{
return array(
'connector' => array(
'class' => 'ext.elFinder.ElFinderConnectorAction',
'settings' => array(
'root' => Yii::getPathOfAlias('webroot') . '/uploads/',
'URL' => Yii::app()->baseUrl . '/uploads/',
'rootAlias' => 'Home',
'mimeDetect' => 'none'
)
),
);
}
}
and i created one more function for rendering the index page(i want the file manager to be in this page)
in the view i wrote the following code
$model = new xxxmodel();
$this->widget('ext.elFinder.ElFinderWidget', array(
'model' => $model,
'attribute' => 'serverFile',
'connectorRoute' => 'admin/elfinder/connector',
)
);
and i included a div for containing it
But i am getting the following error
Alias "ext.elFinder.ElFinderWidget" is invalid. Make sure it points to an existing PHP file and the file is readable.
i tried to include alias in config/main.php
I know i am messing some where with the folder structure
here is the path i am using the extension
C:\xampp\htdocs\project\protected\extensions\ext.elfinder
I returned empty after google searches, can any one please explain me how to use this extension to place the code exactly where it is needed to be?
In general the extensions folder already has the ext alias, so you don't need to set an alias for it.
Then the extension itself should be placed in the extensions folder, somewhat like : project/extensions/extension-name/ . In your case it should be: project\extensions\elFinder , and keep the rest of your code same, i.e continue referring to extension like:
ext.elFinder.ElFinderWidget