I'm new to Sails and seems that I cannot find an elegant way to use multiple layouts with sails v.10.
My site has two different sections: public website and admin side. I don't want to set the layout separately for every action for different controllers in the app's admin section. It seems that policies might be the way to handle this but for some reason it's not working and couldn't find details how to handle this from Sails's documentation or around the web.
I think the following way of doing it results in repetition:
module.exports = {
index: function(req, res){
res.view({ layout: 'layoutadmin' });
}
}
Any pointers?
Thanks,
J
res.locals.layout = 'layoutadmin';
This should work inside a policy. If it doesnt, problem is somewhere else.
Related
I am working on multi-tenant approach. I created a new tenant with all its information in database including its sub-domain. My approach is separate database and separate sub-domains for each customer. So, I am creating a sub-domain on button click and saving it in database. Now, I am lost on my way how to redirect that sub-domain to main page. I just need to redirect it to main page nothing else, I will work on separate db connection later. I also created the wildcard subdomain entry from cpanel. I am working on wildcard domains for the first time and also I am on my way of learning laravel.
Here is my code:
routes/web.php
Route::group(array('domain' => '{account}.example.com/tracker'), function()
{
Route::get('/', 'HomeController#index');
});
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
return view('welcome');
}
}
I know there are lot of things missing that's why its not working but I tried many solutions I found on google and stackoverflow but still I am unable to figure out what I am doing wrong. Please help me how to overcome this issue. Also, please if anyone have good tutorial on this topic, please share that with me.
Thank you
I solved my problem by using this solution on stackoverflow.
Allow multiple subdomain in laravel without making subdomain as route variable?
It worked like a charm. Hope it helps someone.
Thank you.
I want to proceed with more advanced queries in a sailsjs and mongodb stack where sailsjs is setup to serve as an api with data against a front end client. I've been able to fetch data with some basic queries but now looking into on how to proceed with more advanced ones. For example, I want to query the database for entries where the string match either the title or the text, something like this,
db.mycol.find({$or:[{"text":/.*test.*/},{"title": /.*test.*/}]})
My question, where do I put this logic? Any hints, links, tutorial that could point me into the right direction for this would be appreciated.
If you generate an api, for example:
sails generate api Customer
Sails will create a CustomerController for you in api>controllers.
You can add whatever custom endpoints you want to that.
If I put in CustomerController
blah: function(req, res) {
res.json(200, 'You are at blah');
}
and I navigate to customer/blah, it will hit that endpoint. So you can add whatever custom endpoints you want to for that controller. Go crazy.
Do not forget that sails has policy enforcement that you need to set up in the config/policies.js. This lets you expose, block, or add whatever middleware you need to in order to keep your back end as secure or open as it needs to be.
What I'm trying to do is described in great detail here:
Call a Server-side Method on a Resource in a RESTful Way
I have Ember Data's RESTAdapter working with my API, but now I want to give Ember.js a way to kick off various server-side actions using custom routes, such as /docs/1/share or /docs/1/activate. The former would possibly modify the record but the latter would not.
What's the best way to do this?
TIA!
Ember has jQuery baked in. In your controller:
actions: {
activate: function() {
var docId= this.get('id'), self= this;
Ember.$.ajax({
url: '/docs/%#/activate'.fmt(docId),
// your other details...
}).then(function(resolve) {
self.set('name', resolve.doc.name);
// process the result...
});
}
}
You can also use ic-ajax which is a nice wrapper around jQuery.ajax, you can see an example here using ix-ajax.
I have been tasked with creating a user control to live in our master page that allows users to switch between accounts. This way, we can allow users to change their account without having to go back to the accounts page. This seemed like a legitimate and perfectly straightforward task.
I've built the control and added it to the master page using Html.RenderAction. The last step is for me to redirect the user to the home page for that account. In order to do this, I build a route to the home page and attempt return RedirectToRoute(route).
When I attempt this, I get this error:
Child actions are not allowed to perform redirect actions
Anyone have any ideas on how to resolve this or have I coded myself into a box
Thanks in advance
You can cheat with an ugly hack:
[ChildActionOnly]
public ActionResult SomeUserControlAction()
{
// ... some processing
var url = Url.RouteUrl("routeName", new
{
action = "foo",
controller = "bar"
});
Response.Redirect(url);
return null;
}
It's so ugly that I feel ashamed for even mentioning it, but it works.
Another possibility would be to pass the url as part of the view model to the view and perform the redirect in javascript by setting window.location.href to the new url.
I'm new to ZF and need to create multiple login views for each of my 3 user types, employees, employers and admins. Should I use the indexcontroller to serve up the login for the employees and create separate controller classes to handle the employer and admin login pages? How might I utilize JQuery to direct my employer and admin users to the correct login page from the index view?
Thanks much:)
I can give you 2 options.
Modules
Split your Application into logical segments called modules, for those 3 groups each group will receive its own Module.
Each module mimics the well known standard "Application" structure:
module
Controllers
Models
etc
ACL
http://framework.zend.com/manual/en/zend.acl.html
You check which type of user is currently logged and decid via "if()" statements which view should be rendered.
Custom view rendering is done as described by "Lobo":
via
$this->_helper->viewRenderer->setRender('view-name');
If you don't have any user session data, I mean, if you absolutely do not know of which kind the user visiting your page is you simply have to serve 3 links to either a different module or different controller or to one and the same controller but passing the user type as param.
Examples:
Link to module: /modulename/controllername/actionname/
Link to certain controller: /emplyeecontroller/login
Link to general controller handling different params: /logincontroller/login/type/emplyee
There are many possible solutions to achieve your desired aim.
You have to decide which one fits the most into your project.
I would say that this is a bit to open ended to answer in a good way, but I'll try to fill in the blanks with my imagination and give you an answer. I don't use JQuery so I can't give you an answer there unfortunately.
If this is just to handle login I would guess that the logic is more or less the same (and even if it isn't the logic should be in models anyway), and you just want to change the visual appearance, so then you could use the code
$this->_helper->viewRenderer->setRender('view-name');
This code will render the view called /application/views/scripts/controller/*view-name*.phtml by default. Thus you can get whatever variable you use to distinguish the different users and give them the right view.
If there's more differences than just the visual I would probably use different actions within a loginController or something like that.
Then I would use standard indexAction (and thus the view index.phtml as default) for the normal employees, and on that page show some kind of text like "Not an employee? Go to the employers login instead". Employers are then directed to login/employer or something like that which by default will call the employerAction and use the employer view. And then you do something similar with the admin login. the controller will then look something like this
<?php
class LoginController
{
public function indexAction()
{
/*Do login stuff here*/
}
public function employerAction()
{
/*Do login stuff here*/
}
public function adminAction()
{
/*Do login stuff here*/
}
}
Lastly, if there are major differences between how the different users interact with your page, you might consider looking into modules.
You can find all this information at http://framework.zend.com/manual/en/manual.html