Kohana 2.3/3 routing? - kohana

I've been thinking how to make something like this in Kohana:
domain.com/variable
the "variable" is the identifier for a user.
So, is this possible?
http://domain.com/username/controller/action
If yes, can you point me to the right direction, please?
Thanks.

What happens if you have a page named "contact" and a user signs up with "contact" as a username. Which page will be displayed?
Here is an example I threw together for you.
// Pages that aren't users.
Route::set('static', '<action>', array('action' => 'sitemap|faq|terms|privacy|credits'))
->defaults(array(
'controller' => 'static'
));
// User routing
Route::set('user', '<username>(/<controller>(/<action>))')
->defaults(array(
'controller' => 'user',
'action' => 'index'
));
So when this URL is called
http://example.com/sitemap
the first route is used and when
http://example.com/arnold
is called your user class and index method would be called. You can access the username variable with:
$this->request->param('username');
If you have any more questions, feel free to ask.

Related

MvcSiteMapProvider preservedRouteParameters Usage

I have a route defined on my controller action
[Route("xxx/{xxxId:int}/yyy/{yyyId:int}/Details/{editable:bool}")]
I have used 'preservedRouteParameters' and managed get the breadcrumb showing
My mvcSiteMapNode is like this:
mvcSiteMapNode title="TheTitle" controller="yyy" action="Details" preservedRouteParameters="xxxId, yyyId, editable"
I'd like to have the breadcrumb url contain the Id for the previous stage. i.e. xxxId.
Can anyone help with how to achieve this?
[edit]
Having played with it a little more it seems that the problem I have is that my 'id' is not named consistently.
In the parent I have 'id' and the sub page I have 'xxxId'
Changing them both to be just 'id' seems to have worked.

Yii2 / nav bar / Hiding property of non-object

In old version of Yii this part of navbar code is working while user is not logged in.
array('label'=>Yii::t('ge',Yii::app()->user->name), 'url'=>array('/site/index'), 'visible'=>!Yii::app()->user->isGuest),
In Yii2
['label' => Yii::$app->user->identity->username, 'url' => ['site/index'], 'visible'=>!Yii::$app->user->isGuest],
Throws "Trying to get property of non-object" meaning that object Yii::$app->user->identity->username does not exists.
How can I fix this problem ? And why old version is working OK ?
You should simply use :
'label' => Yii::$app->user->isGuest ? 'Guest' : Yii::$app->user->identity->username
Or extends \yii\web\User to handle this.
In Yii2 :
#property IdentityInterface|null $identity The identity object associated with the currently logged-in user. null is returned if the user is not logged in (not authenticated).
In Yii1, CWebUser provides default name. Feel free to ask this feature for Yii2 here : https://github.com/yiisoft/yii2/issues
EDIT : if you just want to hide this menu item for guests, add this to your item :
'visible' => !Yii::$app->user->isGuest,

What URL does this route match?

“routes.MapRoute("static",
"welcome",
new { controller = "Home", action = "index" });”
So does it mean go to Home/Index? What is "welcome" in the URL?
First is the name of the route, second is the url and the third part is the default values.
Please refer to the official documentation next time, for instance this tutorial:
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-custom-routes-cs
This specific route would apply only for url: {root}/welcome , where {root} may be something like www.mysite.com and it will use controller with name Home and activate its action Index.

Should you have an index action for view controllers?

For a lot of my view controllers, I do not have an index action. Is this bad practice?
After looking through a lot of sample applications I see that almost all applications that don't use an index action, at-least declare the following:
public function action_index()
{
$this->request->redirect('');
}
Is it best practice to at-least have this index redirect for each view controller or is it perfectly acceptable to not even declare such method as I am currently doing?
You don't need to declare action_index method.
Simply set default action for route to something else than "index". Example
Route::set('default', '(<controller>(/<action>(/<param>)))')
->defaults(array(
'controller' => 'welcome',
'action' => '*your_action*',
));

change layout content from controller

I am using Yii. I want to have a dynamic link on a layout. This dynamic link will be modified by controllers. Let's say that dynamic link uses a user's id given by controllers to perform a task.
I am thinking to use jQuery script to get user id returned by controllers then use the user id to modify a div that holds the dynamic link.
What do you think about this technique?
It seems like you want to dynamically change a link AFTER the page is rendered, with client-side JavaScript. But it makes more sense to dynamically render a different link the first time, during the server-size PHP rendering process. The controller generates the view, after all! I would get the user ID from the controller during the page request, pass the ID in to the view, and then build the link in the view dynamically on the initial page load.
If you are modifying a link in a layout (not a view), then the best thing to do is create a variable in the Controller, and set that variable with the view. Look at how Yii uses the $layout, $menu and $breadcrumbs variables to do this.
Assuming that the user is logged in and you want their ID, you can get the ID from the Yii::app() object as well, like so:
<?php echo CHtml::link('Edit user',array('user/edit','userId'=>Yii::app()->user->id)); ?>
But at that point, you can just request the user's ID in the controller, and don't need to build a link like this.
Assuming that you want a different user ID than the logged in user, pass that ID ($userId) from the controller into the view, and just do this (as Moyersy said):
<?php echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>
This will build the following link (where $userId = 99999999):
Edit user
So when the linked is clicked, in the actionEdit() you now have access to the user's ID via the GET variable $_GET['userId'].
NOW, if what you want to do is change an already created link, then you would need to use jQuery. But you will need to explain in more detail why you are doing this and what is triggering the link change (a dropdown menu?).
I'm sorry, I can't understand what you are trying to do. Specifically I don't understand what a dynamic link is.
Edit:
<? echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>

Resources