Same URL whether called from module or not - yii

If I use Yii::app()->createUrl('user/login') i will get the result as '/user/login'
But I will use Yii::app()->createUrl('user/login') inside a module i will get '/module/user/login'
Is there any way so that I can '/user/login' irrespective of from where createUrl is called?
I am using a common function which returns the url.

Yii::app()->createUrl('//user/login')
will work for you. The // refers to the application root. To access controllers inside other modules you can use //module/controller/action

Related

Yesod.Auth no authentication and no authorization

I'am writing a web app for personal use with Yesod. I don't want authentication and no log in.
What is the best way to achieve this?
remove the Auth library from the application
use a default hidden user that automatically logs in at startup (Auth.dummy?)
or something else....
Assuming you're using a recent version of the scaffolded site, you should be able to look in src/Foundation.hs for the definition of isAuthorized. Replace the entire definition with:
isAuthorized _ _ = return Authorized
or even remove it entirely, since the above definition is the default.
That should be sufficient to allow access to all pages. Next, search your code for uses of maybeAuth* and requireAuth* functions. Make sure that pages that use maybeAuth* work as expected if they get back Nothing. Remove any uses of requireAuth* and any dependencies on its return value.
Afterwards, you can clean up unneeded code, but this is entirely optional:
In Foundation.hs, you can:
remove Yesod.Auth.Dummy and Yesod.Auth.OpenId imports
remove the definition of muser <- maybeAuthPair from defaultLayout
remove login/logout/profile pages from the navbar (menuItems)
remove the authRoute definition in the instance Yesod App
remove AuthR and ProfileR from the breadcrumb
remove the instance YesodAuth App, the definition of isAuthenticated, and the instance YesodAuthPersist App
In NoFoundation.hs, you can remove the Yesod.Auth import.
In Settings.hs, remove the appAuthDummyLogin field and the reference to it in instance FromJSON AppSettings
In config/routes.yesodroutes remove /auth and /profile routes
Remove src/Handler/Profile.hs and the import Handler.Profile from Application.hs.
Stamp out any remaining references to maybeAuth* functions or references to the ProfileR route.

Disable action method from being called from the address bar

I have a method in my controller that I don't want to be called from the address bar in the browser...
Is there a way to do that? Maybe some kind of annotation, modification in the route configuration? Which are my options?
If you are going to use this action only from within your controller or Views then you can use ChildActionOnly attribute.
If you want to access it using POST then you can use [HttpPost] attribute.
But if you wish to use it using GET (i.e. using AJAX call etc) and don't want users to access it using address bar then you can follow this tutorial to make your actions AJAX only.
Or, if you simply want a method that is not an Action at all (i.e. cannot be called using HTTP) then you can either make it private or use [NonAction] attribute
Use NonAction attribute on the method.

How to add javascript to YII correctly?

I want to create several javascript function that will be needed on different pages. Most will be relevant only to one page, but some to several. I know if I add general conversion functions, it would be a good idea to just create a new javascript file and put all these generic functions into that one file. Bringing me to my first question:
Where would you store the generic javascript file? In "protected"? Which subfolder?
Then, I need to address the placement of other javascript code.
If I have javascript that will only be used on one page, should I use this technique or should I stick to a similar approach as above?
The emphasis is on doing it correctly. I want to fall exactly in line with the yii framework.
store your generic javascript file in your_app/js folder
i.e js folder is at same level to protected.
if js is only used on one page than it will be better not to use generic file.
The best way to have you generic js code into /js/ or similar named folder under root of your app code. Personally I would separate my custom code files into one other subdirectory /js/custom/ and /js/vendors/ where in this vendor folder you can put ready js code such as jquery plugins etc.
Also don't forget to set this path to config file like this:
'components'=>array(
'clientScript' => array(
'coreScriptUrl' => 'path/to/js/lib/dir',
'enableJavaScript' => true,
),
),
where path/to/js/lib/dir is your final js folder name path

Yii generate errors from parent controller

I just started using Yii, coming from Codeigniter (CI). I'm trying to set up a situation where the application will check a users credentials before they can even access the site. In CI, I would create a parent controller, put my checks in there, and then inherit that parent controller. I've been trying to do the same thing with Yii, but with no luck.
I first created an init() method (for some reason I can't put the code in a __construct() method). I then perform my check, which works fine. I can throw a new CHttpException, but that looks ugly. How do I use Yii's built in error handling to accomplish what I want to do?
For simple login rules you should just implement the 'accessControl' filters from Yii see Yii documentation on authorizations.
Another way would be to throw the Exception like you already did, and write custom Http error views, place it in the yerfolder/protected/views/system/ (see the yii/framework/views directory for examples)
I solved this by sending the error to the site/error view and then calling Yii::app()->end() to keep the child controllers from loading.

How can I display Joomla modules within a component?

I have a component with several categories of games and I want to display a different banner depending on the category. I need the banner (and link) to be customizable so it's not simply a case of showing categoryX.jpg or whatever.
I saw this Joomla help page on another question, but it only seems to display a module type, not one specific module. I'd like to use various mod_custom modules and display the one appropriate to the category (I can store each module's ID if necessary).
Any ideas?
This can be done in a 2 step process:
Use the "Load module into article" plugin to allow yourself to access the module using a plugin call like:
{module [*mod_id*]} // where *mod_id* is the module id
Now that you can call a plugin to put your module anywhere, you now need to go to the view code of your/a component you wish to add the module to and add this code:
echo JHTML::_('content.prepare', '{module [*mod_id*]}');
Please see this link - http://www.alltogetherasawhole.org/group/developers/forum/topics/running-joomla-content-plugins - regarding point number 2. I was trying to do the same thing and I found it didn't work, and the reason for this was a comment you can find on the page link above:
"I noticed that some plugin actually expect option=com_content for them to process onPrepareContent properly, so you might actually want to "fool" them by callin JRequet::setVar and restoring the correct values after the trigger."
If you would like to show module within PHP code then use this code, it's worked for me :
<?php echo JHTML::_('content.prepare', '{loadposition position-2}'); ?>
If you want to show module inside html content then just put this code where you want to show module.
{loadposition position-2}
It loads all modules which are assigned to position-2.
You can also use the Joomla Module Renderer
$doc = JFactory::getDocument();
$renderer = $doc->loadRenderer('modules');
$position = 'custom_position_name';
$options = array('style' => 'raw');
echo $renderer->render($position, $options, null);
Just a heads up, Along with being assigned the module position, that module also has to be set to "Display on All pages".
Joomla has this functionality built in. You can use the {loadposition XX} command within your content items. You can specify custom module positions that do not appear in your template to insure that you have the correct module in the correct article.
http://docs.joomla.org/How_do_you_put_a_module_inside_an_article%3F
You could create a new module?
If each category appears in the querystring (catid=X) then you could take a similar approach to https://support.pillwax.com/open-source/doku.php?id=joomla:header_image.
Alternatively, that link might actually do it for you.