How to set up CodeIgniter controllers for similar forms in different directories
I'm using CodeIgniter to access a variety of form types
I have a directory such as this:
-views
--resources
---app1
----form.php
---app2
----form.php
---app3
----form.php
---app4
----form.php
My class is currently very basic, but this
class Resources extends CI_Controller {
public function app1($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app1/form.php', $data);
$this->load->view('templates/footer', $data);
}
public function app2($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app2/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:
class Resources extends CI_Controller {
public function view($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $folder. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?
php codeigniter
add a comment |
I'm using CodeIgniter to access a variety of form types
I have a directory such as this:
-views
--resources
---app1
----form.php
---app2
----form.php
---app3
----form.php
---app4
----form.php
My class is currently very basic, but this
class Resources extends CI_Controller {
public function app1($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app1/form.php', $data);
$this->load->view('templates/footer', $data);
}
public function app2($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app2/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:
class Resources extends CI_Controller {
public function view($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $folder. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?
php codeigniter
add a comment |
I'm using CodeIgniter to access a variety of form types
I have a directory such as this:
-views
--resources
---app1
----form.php
---app2
----form.php
---app3
----form.php
---app4
----form.php
My class is currently very basic, but this
class Resources extends CI_Controller {
public function app1($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app1/form.php', $data);
$this->load->view('templates/footer', $data);
}
public function app2($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app2/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:
class Resources extends CI_Controller {
public function view($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $folder. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?
php codeigniter
I'm using CodeIgniter to access a variety of form types
I have a directory such as this:
-views
--resources
---app1
----form.php
---app2
----form.php
---app3
----form.php
---app4
----form.php
My class is currently very basic, but this
class Resources extends CI_Controller {
public function app1($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app1/form.php', $data);
$this->load->view('templates/footer', $data);
}
public function app2($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/app2/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
This seems very verbose and unnecessary to have a method for every form. However I can't find how I can change the directory without creating a new method. I would ideally like a method where I can pass in a new directory as an arg like $page can be. Eg:
class Resources extends CI_Controller {
public function view($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $folder. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
}
However, it seems CodeIgniter doesn't allow this. Can anyone suggest a way in which this can work?
php codeigniter
php codeigniter
asked Nov 22 at 20:44
prikkles
365
365
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.
public function app_form($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $page. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
That should work but how are you going to call the functions? Via the routes.php file?
That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
– prikkles
Nov 25 at 20:53
add a comment |
Actually you can.
Create a base_controller
inside your core
folder and call it MY_Controller.php
and make it extends CI_Controller
and create a method inside MY_Controller
and name it render, render_view, view
whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
public function render_view($view = '')
{
$this->load->view('templates/header', $this->data);
$this->load->view('view_path/'. $view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
and for every controller in your application make it extend MY_Controller
and whenever you wanna render a view use render_view($view)
and you got you header
and footer
preloaded, and that's the simplest way of making it DRY.
Finally in your controller it should be like this:
class Resources extends CI_Controller {
public function app1($page = '')
{
// $data array in my_controller, it will automatically be passed inside render_view
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app1/form');
}
public function app2($page = '')
{
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app2/form');
}
}
Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
– prikkles
Nov 26 at 6:57
You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on thecontroller name represents view dir name
andmethod name represents view or form name
– Sherif Salah
Nov 26 at 11:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437784%2fhow-to-set-up-codeigniter-controllers-for-similar-forms-in-different-directories%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.
public function app_form($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $page. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
That should work but how are you going to call the functions? Via the routes.php file?
That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
– prikkles
Nov 25 at 20:53
add a comment |
Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.
public function app_form($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $page. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
That should work but how are you going to call the functions? Via the routes.php file?
That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
– prikkles
Nov 25 at 20:53
add a comment |
Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.
public function app_form($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $page. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
That should work but how are you going to call the functions? Via the routes.php file?
Calling your function "view" is almost certainly a bad idea... it's used by CI for the $this->load->view() for starters.
public function app_form($page = '')
{
$data['title'] = ucfirst($folder); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('resources/'. $page. '/form.php', $data);
$this->load->view('templates/footer', $data);
}
That should work but how are you going to call the functions? Via the routes.php file?
answered Nov 22 at 20:58
AlunR
425310
425310
That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
– prikkles
Nov 25 at 20:53
add a comment |
That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
– prikkles
Nov 25 at 20:53
That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
– prikkles
Nov 25 at 20:53
That's an oversight. I just wrote that as an example of a method I'm looking to have - I wouldn't call in view in my app.
– prikkles
Nov 25 at 20:53
add a comment |
Actually you can.
Create a base_controller
inside your core
folder and call it MY_Controller.php
and make it extends CI_Controller
and create a method inside MY_Controller
and name it render, render_view, view
whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
public function render_view($view = '')
{
$this->load->view('templates/header', $this->data);
$this->load->view('view_path/'. $view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
and for every controller in your application make it extend MY_Controller
and whenever you wanna render a view use render_view($view)
and you got you header
and footer
preloaded, and that's the simplest way of making it DRY.
Finally in your controller it should be like this:
class Resources extends CI_Controller {
public function app1($page = '')
{
// $data array in my_controller, it will automatically be passed inside render_view
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app1/form');
}
public function app2($page = '')
{
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app2/form');
}
}
Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
– prikkles
Nov 26 at 6:57
You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on thecontroller name represents view dir name
andmethod name represents view or form name
– Sherif Salah
Nov 26 at 11:13
add a comment |
Actually you can.
Create a base_controller
inside your core
folder and call it MY_Controller.php
and make it extends CI_Controller
and create a method inside MY_Controller
and name it render, render_view, view
whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
public function render_view($view = '')
{
$this->load->view('templates/header', $this->data);
$this->load->view('view_path/'. $view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
and for every controller in your application make it extend MY_Controller
and whenever you wanna render a view use render_view($view)
and you got you header
and footer
preloaded, and that's the simplest way of making it DRY.
Finally in your controller it should be like this:
class Resources extends CI_Controller {
public function app1($page = '')
{
// $data array in my_controller, it will automatically be passed inside render_view
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app1/form');
}
public function app2($page = '')
{
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app2/form');
}
}
Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
– prikkles
Nov 26 at 6:57
You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on thecontroller name represents view dir name
andmethod name represents view or form name
– Sherif Salah
Nov 26 at 11:13
add a comment |
Actually you can.
Create a base_controller
inside your core
folder and call it MY_Controller.php
and make it extends CI_Controller
and create a method inside MY_Controller
and name it render, render_view, view
whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
public function render_view($view = '')
{
$this->load->view('templates/header', $this->data);
$this->load->view('view_path/'. $view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
and for every controller in your application make it extend MY_Controller
and whenever you wanna render a view use render_view($view)
and you got you header
and footer
preloaded, and that's the simplest way of making it DRY.
Finally in your controller it should be like this:
class Resources extends CI_Controller {
public function app1($page = '')
{
// $data array in my_controller, it will automatically be passed inside render_view
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app1/form');
}
public function app2($page = '')
{
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app2/form');
}
}
Actually you can.
Create a base_controller
inside your core
folder and call it MY_Controller.php
and make it extends CI_Controller
and create a method inside MY_Controller
and name it render, render_view, view
whatever you want and inside that function load you layout partials and template and just pass the view to it: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
public function render_view($view = '')
{
$this->load->view('templates/header', $this->data);
$this->load->view('view_path/'. $view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
and for every controller in your application make it extend MY_Controller
and whenever you wanna render a view use render_view($view)
and you got you header
and footer
preloaded, and that's the simplest way of making it DRY.
Finally in your controller it should be like this:
class Resources extends CI_Controller {
public function app1($page = '')
{
// $data array in my_controller, it will automatically be passed inside render_view
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app1/form');
}
public function app2($page = '')
{
$this->data['title'] = ucfirst($folder); // Capitalize the first letter
$this->render_view('app2/form');
}
}
answered Nov 23 at 13:15
Sherif Salah
631512
631512
Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
– prikkles
Nov 26 at 6:57
You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on thecontroller name represents view dir name
andmethod name represents view or form name
– Sherif Salah
Nov 26 at 11:13
add a comment |
Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
– prikkles
Nov 26 at 6:57
You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on thecontroller name represents view dir name
andmethod name represents view or form name
– Sherif Salah
Nov 26 at 11:13
Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
– prikkles
Nov 26 at 6:57
Thanks for this - I'm going to use this so I don't have to load header and footer in every method. However, I'm looking for a way to load all the forms using one method. If I use a method per form, my methods could be unlimited (well into the hundreds) and that seems unnecessary when I just need to change the directory of the form in the method when loading the view.
– prikkles
Nov 26 at 6:57
You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the
controller name represents view dir name
and method name represents view or form name
– Sherif Salah
Nov 26 at 11:13
You can achieve that here, i'm doing that myself .. i don't even have to use render_view unless i'm changing the template, but it takes a lot more code inside base controller to achieve that but i'll give you a hint, you can render the view based on the
controller name represents view dir name
and method name represents view or form name
– Sherif Salah
Nov 26 at 11:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437784%2fhow-to-set-up-codeigniter-controllers-for-similar-forms-in-different-directories%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown