Codeigniter my helper function - function

first of all sorry if its a stupid question but I'm a beginner, and I would like the opinion of a more experienced developer.
I'm building a real estate site with codeigniter, and I have city names what are accented characters, but I'm sending these in the database non accent because of the search, and because of the other validations.
So when I select these they are non accented chars, I created a helper what converts these back accented.
My question is, is it ok if I run this helper in my view?
I mean like, the helper is auto-loaded because I will need this in 4 view files, and I call its function like this in the view
echo city_accent($e->city);
so is it a problem if I run it like this in my view file?
Thank you

Calling a helper function in view can be done, but better way as per the convention would be to call it in controller and pass it to view form controller.
like:
$data['someVar'] = city_accent($e->city);
$this->load->view("your_view", $data);
Hope it helps

Technically, you can do that. As long as that helper is loaded in the Controller that calls the View.
I wouldn't put it in the View however. Layout and logic need to be separated in an MVC environment. I would use the city_accent() function in my Controller and pass the result into the View by parameter.

Related

Switch between 2 or more templates with an action in controllers?

I have a default Phoenix application. This app will have a page_controller
which will load an index.html.eex file.
The app will know to use the view to access templates/page/index.html.eex.
Now say you have created another html page which is identical to index.html.eex in every way except it is in French.
As we do not want to create a whole new Phoenix application which will have all the same code, with the exception being the French translation of the current page/index.html.eex, is there a way to tell
the view or the controller which file needs to be loaded.
Is there a plug which can be placed in the router to alter where render will look for it's templates?
First of all I would suggest you to use Gettext to use labels for French pages.
For example you can all French templates keep in the very same folders (to don't change logic for view), but to name them with suffix eg. "index_fr.html.eex" etc. and then you can write quite simple helper (not necessarily a plug) that will add to all of your templates this suffix.
Still, I would recommend you using Gettext - template's source code is only in place and almost all of the logic Gettext handles for you.
I suggest you pick the #patnowak's answer. Use Gettext, that's the tool made for translation and is powerful enough.
If you still want to do it, remember render/3 in controller calls render/2 functions defined in views, if defined. If not, it runs default rendering function and looks for the template. Read docs for more information.
So for example, this is the controller:
def index(conn, params) do
# defined assigns as you wish
render(conn, "index.html", assigns)
end
Now, define this in the view:
def render("index.html, assigns) do
case assigns[:lang] do
"fr" -> render("index_fr.html", assigns)
_others -> render("index_en.html", assigns)
end
end
You may also write a plug to automatically put :lang into assigns:
def lang_plug(conn, opts) do
conn
|> fetch_query_params()
|> (fn cn -> assign(cn, :lang, cn.query_params[:lang] || "en").()
end
Look Plug.Conn to see docs of fetch_query_params/1 and assign/3, and also other functions to fetch language from other places like headers or body.
You get the idea. In the plug, fill assigns with :lang, fetch them inside your defined render function and act appropriately.
Still, Don't do this. Using Gettext is the proper way.

How to split up yii2 controller

Im using yii2 and php presentation to create powerpoint files.
The point is...
I have a ExportController that has data intructions to make data useful for phppresentation. But it also has grafic instructions to create files and download them.
So ExportController is huge bunch of lines because grafic intructions take a lot of code lines.
What I want to do is to split up grafic instructions from data instructions.
How can I send data from actionConsultar (it is ExportController) to actionGenerar (Im planning it to be in GraficsController).
If you know a better way to do this feel free to comment, all suggestions are welcome.
The right way is create a proper model and add common function to this model so you ca refer to the function in all the action of your controller passing simply the param data .. when you create and manipulate the model ..
A second useful way is based on a collection of helper function located in a common helper class.
You can define a proper area and assign the right namespace to you eg: GraphicHelper.php containing a class GrapichsHelper with the funcion you need so you can import this function simply adding a use GraficsHelper; when you need some function

Laravel Equivalent of passing variables to functions in codeigniter

I am using CodeIgniter from 2 years and i am trying to move to Laravel and I saw many tutorials of how to use Laravel but i couldn't find any answers to how to pass variables to functions using the URL and without using routes like in CodeIgniter if i called this link
site.com/users/edit/12.
I would be calling the users controller , the edit function and passing a variable of value 12
how can i do the same in Laravel without using routes for every single function or using query strings which will make the URL ugly?
Here is an example.Hope that it will be helpful.......
route.php
Route::controller('users','UsersController');
UsersController.php
class UsersController extends BaseController
{
public function getEdit($value)
{
echo $value;
}
}
url
site.com/users/edit/12
output
12
In Laravel - 3 you may do it using something like this:
Route::controller(Controller::detect());
But in later versions it's not available and you should explicitly declare the routes. Actually it's better to declare routes explicitly and this approach has many benefits too. This is a common problem that happens to developers who migrates from CodeIgniter but later they get motivated and if you use this you'll love this for sure.
You may read this article by Phil Sturgeon who was the man behind the CodeIgniter framework, he discussed about it's down sides of this (automatic routing).
If you are using Laravel - 4 then check the Controllers Manual, specially check out the RESTful and Resourceful controllers section, it may attract you but be familiar with explicit route declaration first.

Angular - building a "public" function (newbie)

I'm After several days learning angularJS through converting my standart JS app to a ng one.
I was wondering about this simple scenario:
I have a global function called fb_connect(),
it can be used from any page (or any controller if you like) to make a facebook-based login.
This function makes a simple http call and receives a JSON object contain data to move on (display a pop up, login, etc...)
I read that I can define a Factory or a Service for my app and use it in any controller, which works fine.
So, I created a fb_connect factory function.
The problem is that now, in every page (every controller), I have to define that fb_connect in the constructor of every controller - for example :
function welcome($scope,fb_connect){});
What is the proper way to do this kind of actions using Angular without having to define these functions each and every time in every controller?
Thanks
Setting up factories and services is all part of the dependency injection system of Angular. Using that system is great when you need to create things that depend on other injected things. It's a big tree of dependencies. It's also nice for creating singletons, such that everywhere in your code end up using the same instance of some object.
It sounds to me like neither of these benefits apply in your case. I'd suggest just not using Angular's DI for it. You have some function defined globally, just call it directly and skip the DI. There's nothing wrong with that.
Of course you say it makes an Ajax call, so doesn't depend on the Angular $http service?
Your two options are:
Declare the function on the $rootScope
Inject it as a service
My advice is to go with making it a service. The whole purpose of services is explained in the Angular.js docs, just like this quote:
Angular services are singletons that carry out specific tasks common to web apps... To use an Angular service, you identify it as a dependency for the dependent (a controller, or another service) that depends on the service.
As you mentioned in your question, you'd prefer to not define the service in every controller you wish to use it in. With $rootScope you'll be injecting that also in every controller. So really it's a question of which you prefer, although to answer your question, the proper way of using a factory or service is to inject it into the controller you wish to use it in.
You can always put it in the $rootScope
myApp.run(function($rootScope, fb_connect){
$rootScope.welcome = function(){
};
});

Call a controller inside a template and another controller in Fatfree framework

i'd like to know if i can call a controller action inside a template, and inside another controller in fatFree framework (F3).
I'm not sure if i understand you corrently, but calling a Class method in Template would go like this:
{{ MyConroller->doSomething() }}
Of cause you could call one controller within another too... just use raw php
$obj = new MyController();
$obj->foo();
or use the F3 call method $f3->call('MyController->doSomething');
Also check out the new API docs.
http://fatfreeframework.com/base#call
It's still under construction, but hopefully you'll find more information about this or any other framework part very soon.
Many MVC purists would balk at the idea of a View (template) calling methods on the Controller. They would say that the controller needs to provide the data that the view needs, or at least give it the Model, so that it can retrieve data from there.
Furthermore, the View probably shouldn't be doing anything (or asking another component to do anything), other than generating the display. But can query the Model for data. But maybe by doSomething() you do mean getSomeData().
While I'm not an MVC purist I do agree with the idea of keeping logic and functionality out of the view if at all possible.