Is there a way to dynamically include files through the controller, just as you would do include('path_to_file').
The only advice I've gotten was to define it like
$data = array('var'=>'path1','var2'=>'path2');
and pass it through the load view, but this doesn't load the files, so I'm basically back where I started.
Like Repox said, you should follow the MVC structure. Generally, if you plan to include classes (functionality), use models; if you want to include presentation (html), use views. And all this should be done from a controller. For example:
$this->load->model('my_functions');
$this->my_functions->do_something();
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
If you really need it, you can also call models and views from a view.
Hope this clears it up a bit.
Short answer - no.
A more detailed answer would be providing you with details on how to use the MVC correctly.
Splitting up the file you wan't to include into models, views and controllers would be the correct thing to do. That is 'the dynamic way to include files' in CI.
Related
I have a controller named: CustomAuthController. Now I want to create a folder for this controller. What should I choose for this folder name. I mean what name is the best practice for laravel. is custom_auth is best pick. Is there any rule?
View folders in Laravel are made to help the structure of the front end of the app. It has nothing to do with what Controller you use to provide functionality for it.
For instance, you can have the layouts folder where you put all your blades which can be used into another blade as a reusable section. The views under layouts folder might also use your CustomAuthController controller. Does that make sense?
Name your view folders in a way which helps you understand the front end structure of your website.
As for the naming convention, always try to keep it one word, all small case and usually a plural word as it holds a group of blade views.
If you need to make it as two words, mostly snake_case is used but there is no written rule about it.
In a codeigniter project i have to do some set of stuff in one than one controller.
I code all that stuff in a function and now i need to call whenever necessary.
i think Writing this function in more than one controller is not good.
i have 2 options,
create a helper and write these function in that and include the helper in necessary controllers.
Since i have extended CI base controller (My_Controller) and most of my controllers are extended that controller, i can write this function to my base controller also.
I have confused which one is better, right way?
Which one will speed up the process?
Is the second way slows the site?
They are identical for all intents and purposes.
Using a helper allows you to make the code portable, so you can use it in other projects, or to be called from anywhere in the code base, in the case of a formatting function for example
If you were planning to put it in a controller, then MY_Controller is best bet
Just to help you on you endeavor what i do is: (this is just me)
If i need to use something in the views, i use a helper a custom one or built in.
If i want to do something on a controller that other controller will be using too and don't want it to mess up or crowd my controller i use a library (pretty much you can use a helper but i chose to use a library)
If i want to load lets say a method, to affect Globally or some of the controller i use the base controller. (you could also use helper or library)
The key is you are not restricted to one, choose the best that suits you, as the saying goes, there are many ways to skin a cat, but please don't skin a cat..
I'm using Codeigniter to build a web app, and I'm trying to follow proper OOP, MVC and Codeigniter convention. A few things are definitely escaping me. Below is a simplified explanation of what's confusing me.
I have a 'companies' table and a Companies_model. This model has private variables like id, name, and url. This model has a method named load_data( $id ), that uses an id to load all the data about a company into the class variables. This $id is coming from the URL. Finally, this model has public getters that return the values.
I have a profile page on my website for every company in my system. In my Companies controller, I load this company's data in the following way.
$this->load->model( 'Companies_model', 'Companies_profile' );
$this->Companies_profile->load_data( $this->uri_company_id );
When my other logic is complete in the controller, I have the following code to render the view:
$this->data['Companies_profile'] = $this->Companies_profile;
$this->load->view( 'profiles/companies_profile_view', $this->data );
In my view, I render data like this:
<p>This company's name is <?php echo $Company_profile->get_name(); ?>.</p>
All of this seems redundant. Why am I passing data to the view when it's already available in a specific instantiation of the Companies_method? I feel confused and that I'm doing something wrong, but I can't figure out what that is. Perhaps I'm not doing anything wrong, but haven't encountered the reason for some of this structure yet. Overall, my app is fairly simple. Can someone shed light on how to properly organize and pass data to views? Why pass the data, and not just make it all available in CI's base class, $this?
NOTE: I have multiple instantiations of the Companies_model on every page I render because I have a list of related companies on each profile page.
That is MVC, at least the way it's done in CodeIgniter. Separation of displaying values from retrieving them from the database. In such simple cases, the advantages are not really visible.
Imagine you later decide to change the database structure. You would only need to rewrite the model to return the same classes and leave front-end code intact. You can also give the view to some web designer to design, without telling him anything about database structure, etc, etc.
One of the main advantages of MVC shows up when you work in teams. Instead of coming up with your own conventions, everyone can do the stuff uniformly.
Passing a class object to he view breaks the idea behind MVC. MVC is designed to break up the parts of programming into 3 aspects. The Model, View and Controller. What you pass to the view are only things that the view needs. The view probably doesn't need the entire object. It probably only needs certain bits of information from the object. It allows you to keep the business logic away from the visual design. It allows a web designer who knows HTML and a little PHP to go in and make changes without having to know how your objects work. he sees.
<?php echo $company_name ?>
which is a lot easier for a non technical person to interpret than
<?php echo $company->getName(); ?>
using CodeIgniter normally one has to specify the controllers in the config/routes.php file.
This is not to handy, so I would like to be able to do something like this in a controller.
get url parts and check if the first part is specified in an array
if so, load the specified controller, if not, load default controller.
It basically mimics the behavior of the routes file, but there is no need to specify the wildcards before. I am using a base controller I extend with every controller, but I would like to have this controller just load (or include) the needed controller.
Does anyone have any idea how I can do this in a good way?
Thanks in advance.
// Edit
Okay, so here is my scenario.
I have cms and users can choose to include modules (e.g. a gallery).
I need to inlcude all the gallery php scripts without having to have "gallery" in the url.
I figured it would work if I use a "main controller" which loads another controller depending on the modules chosen. I realize this might not be the best way, so if there is a "clean" way to do it, please tell me.
As far as I know models are just for database stuff, so putting a whole gallery in there is not right either. The Plugin itself will of course be a library, but there is going to be some code to load the libs depending on the demands, get the database data, etc.
Thanks
The way you're doing this is incorrect. You never should take over the routing function to do this. What you need is to use some kind of module functionality to include the required methods and models; a module doesn't require to have route-accessible methods, so it's basically a "library" with a model and views.
If I remember correctly there are several plugins that provide this, One was HMVC (google it).
The ideal form would be to load the "Module" on demand from your controller, like you do with the core CodeIgniter Libraries; so lets say you're inside the blog controller action and you want to include the comment module which is used on gallery and on images, you just include the module and call it's methods to get the data which you can then pass to the view as needed; you can even render partials and store them into a variable to pass to your master controller.
Hope this is enough to get you on the right track :)
I may be misunderstanding your question but you want to load your controllers if you go to them and if not you want to go to your default.
If I am understanding correctly you can do a couple things in your routes have a route that takes everything to your default controller.
In your controller have an array of all your controllers then implode the array to a regular expression
$array = [c1, c2, c3, c4];
$str = implode('|', $array);
$regex = "($str)"
now just add your regex to a route
now redirect as you see fit.
But this is really what the routes file is for you you are just dancing around something that should be used.
I'm kind of new to the whole MVC concept, since I just recently started developing a web site using CakePHP framework. Therefore I turn to you asking for how to achieve the following in a best-practice-way.
I want to be able to place a number of pictures in a directory, which then is scanned for all filenames in it. These filenames should afterwards be passed to an arbitrary view, which then loops through all filenames making img tags of them.
It would be nice if this could be done in a general way, so that I could reuse the code for same task but with a different directory name.
I have already tried the following two approaches. Nevertheless, non of these felt like the best way to do it, for some reason.
Create a model with $useTable=false.
Create an ordinary class and import it as a vendor.
What is the best way to achieve what I describe above?
I'm thinking your original idea is the better one, use the Model to traverse/read/write the directory. Think of the File structure as your data source. You can pass the dirname to the model with $this->data and then let it use the File class to retrieve what you need. This would make it portable across Controllers (with loadModel())
Later on down the road if you move your image paths into a DB you only have to re-write the model to take that into account.
I've done the following before: create an e.g ImageManager class and pass it as an IImageManager to the constructor of your e.g ImageController instead of passing a repository. The ImageManager could then do all the work that you mentioned.
EDIT: I did that in .net mvc and don't have experience of CakePHP, but I guess there's a place where you can register the concrete implementation of IImageManager as ImageManager so the framework would know what to pass to the controller's constructor
I would create a helper for this purpose.
Edit: And for trawling through the filesystem use the Folder class from within your helper.