Yii create first test page - php

I am new to yii i have setup of sample yii demo application (default)n now i want to create new page but i am getting 404 error following are the steps i have followed.
1) In site Controller created new action
public function actionTest()
{
$this->render('test');
}
2) created new page in view folder test.php
echo "test";
3) In main.php (config file) added following code
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'test' => 'site/test',
),
),
4) when i run the application with URL
http://localhost/YiiTest/test
then got the error
The requested URL /YiiTest/test was not found on this server.

make sure you have rewrite your .htaccess file link

Related

404 router in Lithium PHP

I know how to make response routes in the actual /config/routes.php file, but I can't find where to change the default 'fetal dispatcher' error. I'd like to be able to have it route to a nice 404 page I've made when there's a missing page/action controller. Is that possible?
Yes, you can take advantage of lithium\core\ErrorHandler for this. See the code in the default config/bootstrap/errors.php:
ErrorHandler::apply('lithium\action\Dispatcher::run', array(), function($info, $params) {
$response = new Response(array(
'request' => $params['request'],
'status' => $info['exception']->getCode()
));
Media::render($response, compact('info', 'params'), array(
'library' => true,
'controller' => '_errors',
'template' => 'development',
'layout' => 'error',
'request' => $params['request']
));
return $response;
});
This is saying, if any exception occurs during Dispatcher::run(), display the development.html.php template from the views/_errors folder with the layouts/error.html.php layout.
So you can change that -- maybe you check the Environment to see if this is a dev or production environment and display a different template for production.
Maybe if $info['exception']->getCode() === 404, you can switch to a template specifically for 404 errors.

Add admin prefix in usermgmt plugin in cake php

i want make separate admin login but not working giving me error for below url
sitename/admin/admin_login
Error:
Error: AdminLoginController could not be found.
Error: Create the class AdminLoginController below in file: app/Controller/AdminLoginController.php
Cake Php routing setting mention below
app\Plugin\Usermgmt\Config\routes.php
Router::connect('/login', array('plugin' => 'usermgmt', 'controller' => 'users', 'action' => 'login')); // working fine
Router::connect('/admin_login', array('admin' => true, 'plugin' => 'usermgmt', 'controller' => 'users', 'action' => 'admin_login')); // not working
app\Config\routes.php
Configure::write('Routing.prefixes', array('admin'));
app\Plugin\Usermgmt\Controller\UsersController.php(action in controller)
public function admin_login() {
// here is admin login code
}
My view file path
app\Plugin\Usermgmt\View\Users\admin_login.ctp
Your route is set up for /admin_login, NOT /admin/admin_login, like the URL you say you're trying.
Just change your URL to sitename/admin_login and it should work.

Yii, controllers in different subfolders

I have folder hierarchy
---protected
------...
------controller
---------admin
------------OneController.php
------------TwoController.php
---------user
------------ThreeController.php
------------FourController.php
also i add them in main.php as
'import' => array(
'application.models.*',
'application.forms.*',
'application.components.*',
'application.fetcher.*',
'application.controllers.admin.*',
'application.controllers.user.*', ...
add route for admin
'urlManager' => array(
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
// ...
'admin' => 'admin/one/index',
but this doesn't work, can you help to deal with it? I want do simply routes to subfoldered controllers.
I think your syntax of rule is wrong. Try to use
'rules' => array(
'admin' => 'admin/one/index'
)
I Found solution. My mistake was on controller, I have default (old) OneController in /protected/controllers that's why routes give me 'old' data, when I change name of (old) OneController, trouble gone. And after that route give me new one from /protected/controllers/admin/OneController.
it was like this one
---protected
------...
------controller
---------OneController.php <-- this gives wrong data even error because of routes
---------admin
------------OneController.php
------------TwoController.php
---------user
------------ThreeController.php
------------FourController.php

Allowing only clean URLs in Yii

I have configured the CUrlManager in the config/main.php to use clean URL:
'urlManager' => array(
'showScriptName' => FALSE,
'urlFormat' => 'path',
'rules' => require(dirname(__FILE__) . '/routes.php'),
),
The clean URL function works perfectly, but I would like to prevent the default <controller>/<action> pattern match to occur.
This is my config/route.php:
<?php
return array(
'books' => 'book/index'
);
Now people can go to the same book page by 2 different URLs:
http://www.mysite.com/books
http://www.mysite.com/book/index
I want to disable the second URL pattern. Is this possible?
You can enable useStrictParsing in your url manager component.

Zend Framework 2 - Why i can't override MvcEvent's MatchRoute?

i would like to match all requests where user is unlogged to controller Admin\Controller\Sign and action in. I wrote this code in onBootstrap() method in Module.php file :
if (!$authService->hasIdentity()) {
$routeMatch = new RouteMatch(
array(
'controller' => 'Admin\Controller\Sign',
'action' => 'in'
)
);
$event->setRouteMatch($routeMatch);
}
I don't get any errors, but code doesn't work, why?
The problem here is that the application route event (MvcEvent::EVENT_ROUTE) is triggered after the (MvcEvent::EVENT_BOOTSTRAP).
Which means even if you're setting the route match at the bootstrap level, the application is going to override it with the route match of the request after the MvcEvent::EVENT_ROUTE.
If you want to avoid this overriding you need to add a listener for the route event with a very low priority to make sure it will not be overridden:
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, array($this, 'onRouteEvent'), -1000000);
Note : the onRouteEvent would be the method of your Module class that handles the route event (similar to your code).
If you want to short-circuit your application running at the bootstrap level, what you can do is to send the headers with redirection code to the client:
//get the url of the login page (assuming it has route name 'login')
$url = $e->getRouter()->assemble(array(), array('name' => 'login'));
$response=$e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
$response->sendHeaders();
add a route entry sign_in as below in the routes section of the module.config.php under admin module
'sign_in' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin/sign/in',
'defaults' => array(
'controller' => 'sign',
'action' => 'in',
),
),
),
and call the route in the controller like this
$this->redirect()->toRoute('sign_in');

Categories