I've been trying to find a full explanation on how to custom error pages in Kohana 3.0 and I haven't been lucky so far. So, based on the solution provided by Kohana Guide can anyone show me how to do it?
When I say full explanation I mean, the location of the classes, their names, which ones to extend, full code, and please, a view for one of the errors.
Many thanks.
I suggest you read http://kohanaframework.org/3.0/guide/kohana/conventions. You can work out the files that need to be created for yourself based on the class names and how Kohana autoloads. The beauty of Kohana for me is that it doesn't try and do everything for you and for that reason its is really important to read the documentation in my opinion.
Note: I haven't used Kohana 3.0 in particular but this should apply.
For example, in part 1, is this code:
<?php defined('SYSPATH') or die('No direct access');
class HTTP_Response_Exception extends Kohana_Exception {}
Kohana uses a 'cascading filesystem' so you would add the code shown in 2 to
application/classes/kohana/exception.php
Inside that file you would extend the Kohana exception handler
class Kohana_Exception extends Kohana_Kohana_Exception {
public static function exception_handler(Exception $e)
{
...
}
}
The route shown gets added to your applications bootstrap which is under
application/bootstrap.php
As errors are being routed you can tell what the controller will be:
Route::set('error', 'error/<action>(/<message>)', array('action' => '[0-9]++', 'message' => '.+'))
->defaults(array(
'controller' => 'error_handler'
));
So there will be a controller created at:
application/classes/controller/error_handler.php
This will look like:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Error_Handler extends Controller_Template {
...
public function action_404()
{
$this->template->title = '404 Not Found';
// A view example
$view = View::factory('error/404');
$view->render();
...
}
}
Your views would then (possibly) be placed under:
application/views/error/404.php
The reason the documentation is brief is because the same answers do not apply to everyone. For example the majority of people (I know) use their own templates etc.
Related
I need to override the renderPageButton() method in the Yii2 LinkPager widget. The method documentation specifically says "You may override this method to customize the generation of page buttons" but I can't figure out how to do that. Thanks.
Overriding LinkPager can be done this way:
Create a new file ./widgets/MyLinkPager.php:
<?php
namespace app\widgets;
use yii\widgets\LinkPager;
class MyLinkPager extends LinkPager
{
protected function renderPageButtons()
{
// do whatever you want, it may help to
// copy code from parent::renderPageButtons()
// or even call
return parent::renderPageButtons();
}
}
And then use it this way in your view (see here: https://www.yiiframework.com/doc/guide/2.0/en/output-pagination):
use yii\widgets\LinkPager;
echo LinkPager::widget([
'pagination' => $pagination,
]);
The class you want to override is documented here.
You can override it in the following way:
Create a new directory in your yii2 app root folder, like widgets
Create a new php file (like MyLinkPager.php) and a new class in it (MyLinkPager) which extens yii\widgets\LinkPager
You can use "app\widgets" namespace (i.e. if you are working with the basic yii2 app)
In your class, implement only the function you want to override from the original class
Use your new class wherever you want instead of the original one
I'm probably missing something really obvious here, but is there a function in CakePHP (I'm on 3.8) that returns the name of a controller without creating an instance of the class?
An instanced controller can call this function:
echo $this->name;
But what I'd like to be able to do, is avoid typing the controller name as a string in, say, an HTML->link(); ie a static call something like:
echo $this->Html->link(
'Dashboard',
['controller' => DashboardsController::name, 'action' => 'index']
);
The reason is that I'm refactoring a couple of controllers and am having to find and replace all of those strings by hand. I come from a .Net background and CakePHP is pretty new to me, so if there's a better (more cakeish) way to carry out the refactoring than the question I'm asking, then I'd be really glad to hear it.
Nothing in the documents is leaping out at me, but I've a feeling there should be a simple answer.
The namespace of a class can be retrieved using ::class property. Checkout the following example:
DashboardsController::class // Cake/Controllers/DashboardController
The name without the namespace can be retrieved with ReflectionClass:
$function = new \ReflectionClass(DashboardsController::class);
var_dump($function->inNamespace());
var_dump($function->getShortName());
Shortname can be used to get the class without namespace:
namespace App;
class Test {
public static function name(){
$function = new \ReflectionClass(self::class);
return $function->getShortName();
}
}
var_dump(Test::name());
Checkout the docs: https://www.php.net/manual/en/language.oop5.constants.php#example-186
Reflection: https://www.php.net/manual/en/reflectionclass.getname.php
In a book called Yii2 for Beginners, which is mainly about the advanced template, I have encountered the following unexplained code, which seems relevant to RBAC:
$userHasRoleName = Yii::$app->user->identity->role->role_name;
What exactly does this mean? For example, I guess that this:
Yii::$app->user
refers to this file:
vendor\yiisoft\yii2\web\User.php
Is this correct?
In any case, what does the rest of the code refer to? Specifically:
->identity->role->role_name
In the above User.php file, I have not been able to find anything like "function identity()", so it can't be that. I have found numerous $identity variables, but I don't know which one the code might be referring to. And there is no $role variable at all.
What is this code referring to:
Yii::$app->user->identity->role->role_name;
Yii described magic methods like __get, __set and so on, to get access for inaccessible properties. Oftenly such methods begins from get or set (in Yii implementation it is). To get access to ->identity, \yii\web\User has method getIdentity. This method return identity wich you described in config with identityClass property for user component. Oftenly identityClass is a AR model which implements IdentityInterface.
'components' => [
'user' => [
'identityClass' => 'common\models\User',
]
]
To get access to ->role for example you must to create a new method
namespace common\models;
class User extends ActiveRecord implements IdentityInterface {
public function getRole(){
// if user can have only one role
return current( \Yii::$app->authManager->getRolesByUser( $this->id ) );
}
}
Btw implementation of ->role->role_name may be very different.
I was updating my project from laravel 4.2 to laravel 5.0. But, after I am facing this error and have been trying to solve it for the past 4 hours.
I didn't face any error like this on the 4.2 version. I have tried composer dump-autoload with no effect.
As stated in the guide to update, I have shifted all the controllers as it is, and made the namespace property in app/Providers/RouteServiceProvider.php to null. So, I guess all my controllers are in global namespace, so don't need to add the path anywhere.
Here is my composer.json:
"autoload": {
"classmap": [
"app/console/commands",
"app/Http/Controllers",
"app/models",
"database/migrations",
"database/seeds",
"tests/TestCase.php"
],
Pages Controller :
<?php
class PagesController extends BaseController {
protected $layout = 'layouts.loggedout';
public function getIndex() {
$categories = Category::all();
$messages = Message::groupBy('receiver_id')
->select(['receiver_id', DB::raw("COUNT('receiver_id') AS total")])
->orderBy('total', 'DESC'.....
And, here is BaseController.
<?php
class BaseController extends Controller {
//Setup the layout used by the controller.
protected function setupLayout(){
if(!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
}
In routes.php, I am calling controller as follows :
Route::get('/', array('as' => 'pages.index', 'uses' => 'PagesController#getIndex'));
Anyone please help. I have been scratching my head over it for the past few hours.
Routes are loaded in the app/Providers/RouteServiceProvider.php file. If you look in there, you’ll see this block of code:
$router->group(['namespace' => $this->namespace], function($router)
{
require app_path('Http/routes.php');
});
This prepends a namespace to any routes, which by default is App\Http\Controllers, hence your error message.
You have two options:
Add the proper namespace to the top of your controllers.
Load routes outside of the group, so a namespace isn’t automatically prepended.
I would go with option #1, because it’s going to save you headaches in the long run.
What will the best place in the code to track user's last visit date or any data that should be tracked on each request to application? Is it good idea to extend yii\web\Controller?
You can use a base controller and of course it is a good idea. But there is another approach that is more elegant. You can do like below:
1 - Add a component into your components directory, for example(MyTrackingClass):
namespace app\components;
class MyTrackingClass extends \yii\base\Component{
public function init() {
//SOME CODE HERE
//SOME CODE HERE
//SOME CODE HERE
parent::init();
}
}
2 - Add MyTrackingClass component into your components array in config file:
'components' => [
'MyTrackingClass'=>[
'class'=>'app\components\MyTrackingClass'
],
//other components
3 - Add MyTrackingClass into bootstarp array in config file:
'bootstrap' => ['log','MyTrackingClass'],
Now, you can see everything you wrote in your init() method, will be executed in every request, in every module, controller, action and so on...
Please note that, if you do not need to use Events and Behaviors you can use \yii\base\Object instead of \yii\base\Component