how to change the function name in the url using codeigniter? - php

I want to change the function name in the url, i searched for this a lot but unable to find any solution, can any one help me out what should i do
consider for example i have a url like
http://localhost/codeIgniter_try/index.php/Blog/blogvieww ,
so here "Blog" is the controller name and "blogvieww" is the function name so if i want to change the function name from "blogvieww" to "blogvieww_all" what can i do?
Blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
$this->load->view('blogview');
}
public function blogvieww()
{
$this->load->view('blogvieww');
}
}
?>
blogview.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
blogvieww.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 2nd Blog!</h1>
</div>
<div>
<h1>Welcome to my 3rd Blog!</h1>
</div>
</body>
</html>

Hope this will help you :
Add the below line of code in your route.php
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
Your anchor should be like this :
<a href=<?=site_url('Blog/blogvieww_all');?>

You can set it through routes of CodeIgniter.
Your path for routes will be application/config/routes.php
See this below may help.
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
For more detail: https://www.codeigniter.com/userguide3/general/routing.html

1) You can use URI routes. In routes.php, You can specify like below:
$route['Blog/blogvieww_all'] = 'Blog/blogvieww';
Check here for more info.
2) Write same function again in controller with the name of 'blogvieww_all' .

Change this in your controller:
public function blogvieww()
{
$this->load->view('blogvieww');
}
to
public function blogviewW_all()
{
$this->load->view('blogvieww');
}
You just need to change the name of the function in your controller, to the name you want to display on ur url.
Or
As Pradeep has commented, you can change to routing also. But the best way is to change the function name, unless it is being referenced or called from somewhere else.

Related

Send the function with arguments to the model

I have several index pages
And I put the header page separately
I want the name of each page to be sent to the header
How can I do it, thank you?
model::setnameisset('home');
and model.php page
function setnameisset($name)
{
return $name;
}
Now I want it to be saved in the header page instead of the title tag
<!DOCTYPE html>
<html lang="en">
<head>
<base href="<?= URL ?>">
<meta charset="utf-8">
<!-- <meta http-equiv="Content-Type" content="text/html"> -->
<title></title>
</head>
Now how do I call that function?
See I have several pages like contact page or home page
And I put the header in a page and included it in these pages
The structure is m v c
I just want to send the name of the page or the keyword tag to the header so that it opens with that name during execution.
For example, my index or contact file
<?php model::nametag('homepage'); ?>
<body id="page-top">
</body>
Now I have a model file in the core folder
My controller file is like this
class Controller
{
function __construct()
{
}
function view($viewurl,$data=[],$noincludeheader = '',$noincludefooter = ''){
if ($noincludeheader =='') {
require_once ('views/share-base/header.php'); //panel header
}
require_once ('views/'.$viewurl.'.php'); //panel main page
if ($noincludefooter =='')
{
require_once ('views/share-base/footer.php'); // panel footer
}
}
function model($modelUrl){
require_once ('models/model_'.$modelUrl.'.php');
$classname='model_'.$modelUrl;
$this->model = new $classname;
}
}
And my model file is like this
public function nametag($name)
{
return $name;
}
It's so easy, I just want to be placed in any file, a name should be passed to the header page, I don't know how to use the function.

Routing all page with one controller with codeigniter

I'm a codeigniter newbie.
I'm triyng to create one controller that control every url in my site; I'm going to explain what i mean:
I've this views folder structure:
-front
--parts
header.php
footer.php
index.php
login.php
register.php
so if on my site example.com I want to go on login page, simply I'll go on example.com/login.
In controllers folder till now, I had a controller for every single page (HomeController.php, LoginController.php, RegisterController.php). Now I want to change strategy and have one controller to govern all my pages.
After some research on Google and Stack overflow, I've found this thread:
Strategy to route to pages in codeigniter
So I've decided to follow that hints and I've build Pages.php controller:
defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );
class Pages extends CI_Controller {
function _remap( $method )
{
is_file( APPPATH . 'views/front/' . $method . '.php' ) OR show_404();
$this->load->view( $method );
}
}
and in my routes.php:
$route['default_controller'] = 'welcome';
$route['pages'] = "pages/$1";
$route['(:any)'] = "pages/$1";
But I'm not be able to make it work.
In few words I whant create one controller that automatically create url if i create a new file in views/front folder.
I've readed CI docs and a bunch of question on StackOverflow, I've testing different hints, and maybe an answer somewhere, but I'm not finding yet.
I'm in trouble about this by days, maybe I'm missing something basilar in this concept.
Someone could help me to clearify this process?
You can do as below
$route['default_controller'] = 'welcome';
$route['pages']="pages/index";
$route['pages/login']="pages/login";
$route['pages/register']="pages/register";
Your Controller will be like this:
defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );
class Pages extends CI_Controller {
function _remap( $method )
{
is_file( APPPATH . 'views/front/' . $method . '.php' ) OR show_404();
$this->load->view( $method );
}
public function index() {
$this->load->view('front/index', $data);
}
public function login() {
}
public function register() {
}
}
Your index.php view should like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>XYZ</title>
<?php $this->load->view('front//header'); ?>
</head>
<body>
<!-- topbar starts -->
<?php $this->load->view('front/sections/top-nav.php'); ?>
<!-- topbar ends -->
<div class="ch-container">
<div class="row">
<!-- left menu starts -->
<?php $this->load->view('front/sections/leftmenu.php'); ?>
<!-- left menu ends -->
</div><!--/fluid-row-->
<!-- Ad ends -->
<?php $this->load->view('front/sections/footer.php'); ?>
</div><!--/.fluid-container-->
<!-- external javascript -->
<?php $this->load->view('front/sections/footerjs.php'); ?>
</body>
Or you can be load:
public function index() {
$this->load->view('front/header');
$this->load->view('front/index', $data);
$this->load->view('front/footer')
}

code igniter - loading a controller and its data within another controller / view

I'm a bit confused on including a controller / view within another controller/view. I'm doing the following and getting funny rendering issues:
//CONTROLLER About
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function about(){ //the standalone about page
$data['about_inner'] = $this->about_inner();
$this->load->view('pages/about',$data);
}
public function about_inner(){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
$this->load->view('pages/about-inner',$data);
}
}
//view about.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container about" data-page="about">
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
</body>
</html>
The issue I'm getting is that $about_inner does not end up loading inside of the "scroll-container" - it loads and renders before everything else, as shown in the screenshot.
What's the best way to get the about_inner view and all of its associated data to load within the about view? I need all the content ($this->about_model->get_content()) to come from about_inner since it can also be loaded by other pages via ajax.
SOLUTION
public function about(){ //the standalone about page
$data['nav'] = $this->load->view('templates/nav', NULL, TRUE);
$data['about_inner'] = $this->about_inner(true);
$this->load->view('pages/about',$data);
}
public function about_inner($print =false){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
return $this->load->view('pages/about-inner',$data, $print);
}
//HTML
//view about:
<div class="container about" data-page="about">
<?=$nav; ?>
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
You just need to tell the about_inner function to return the data and not print the data. That's the 3rd argument to function view($template, $data, $return);
$this->load->view('pages/about-inner',$data, true);
If you need to do either or, just set a boolean flag as the argument to the function
function about_inner($print = false){
...
$this->load->view('pages/about-inner', $data, $print);
}
Then when you call the function you can simply pass true to the function in order to get it to return the HTML instead of printing the HTML.
$this->about_inner(true)
if your JS folder is in root you have to define it with base_url() function. So correct all the path in get_content method
Ex
<script src ="<?php echo base_url() ?>path/to/your/folder/aaa.js" .....
And i think you can keep one controller method and remove one.(You can keep about and remove about_inner)

codeigniter load view specific javascript

I have a register view file on which there is a very big form. Hence I plan to use a form wizard plugin along with the jquery form validation plugin. I only want these scripts loaded on specific pages. How do I do this ? Here is my controller method.
public function index(){
$data['title'] = "Register";
$this->load->view("site_header", $data);
$this->load->view("site_nav");
$this->load->view("content_register");
$this->load->view("site_footer");
}
I saw a similar post on stackoverflow here but I don't understand what to do. Please help.
You can send which js to load as a variable. Example is here;
Sample controller
class mypage extends CI_Controller{
public function index(){
$data['js_to_load']="index.js";
$this->load->view('header',$data);
$this->load->view('my_html_page');
}
public function second_page(){
$data['js_to_load']='second.js';
$this->load->view('header',$data);
$this->load->view('my_second_html_page');
}
}
header.php - View file
<!-- Needed html code here -->
<? if ($js_to_load != '') : ?>
<script type="text/javascript" src="assets/js/<?=$js_to_load;?>">
<? endif;?>
<!-- Other html code here -->
Whats happening?
We are setting the js file into $data variable, and passing the $data variable to header.
In header file we are checking if js_to_load is set? If yes, we are including the js file.
Also you can pass multi js files by using arrays.
Sample controller
class mypage extends CI_Controller{
public function index(){
$data['js_to_load']=array("index.js","index1.js","index2.js");
$this->load->view('header',$data);
$this->load->view('my_html_page');
}
public function second_page(){
$data['js_to_load']=array("second.js","second2.js","second2.js");
$this->load->view('header',$data);
$this->load->view('my_second_html_page');
}
}
header.php - View file
<!-- Needed html code here -->
<? if (is_array($js_to_load)) : ?>
<? foreach ($js_to_load as $row):
<script type="text/javascript" src="assets/js/<?=$row;?>">
<?endforeach;?>
<? endif;?>
<!-- Other html code here -->
Pass an array of js you want added to the header in the data, and add in in the view, there are probably other ways of doing it (this kind smears the controller/view line, so it's a bit dirty, but it works).
public function index(){
$data['title'] = "Register";
$data['js'] = array('link', 'link', etc);
$this->load->view("site_header", $data);
$this->load->view("site_nav");
$this->load->view("content_register");
$this->load->view("site_footer");
}
It is better to load your .js in your footer as your HTML should fully load before your start running script over it.
see here for reasons
i use a universal footer to handle the .js files
public function index()
{
$this->load->view('template/header');
$this->load->view('template/nav');
$this->load->view('thing/landing');
$this->load->view('template/footer');
}
my footer view is
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$caller_class = $this->router->class;
$caller_method = $this->router->fetch_method();
?>
<script type="text/javascript" src="<?php echo base_url(); ?>public/js/jquery.min.js"></script>
<?php
//look for JS for the class/controller
$class_js = "public/js/custom-".$caller_class.".js";
if(file_exists(getcwd()."/".$class_js)){
?><script type="text/javascript" src="<?php echo base_url().$class_js; ?>"></script>
<?php
}
//look for JS for the class/controller/method
$class_method_js = "public/js/custom-".$caller_class."-".$caller_method.".js";
if(file_exists(getcwd()."/".$class_method_js)){
?><script type="text/javascript" src="<?php echo base_url().$class_method_js; ?>"></script>
<?php
}
?>
</body>
</html>
using $this->router->class; or $this->router->fetch_method(); to detect what controller or method in the controller is being used. then detecting if a .js file exists for that controller/method
with little management you can have controller and/or method specific scripts without fiddling with the footer.
in file pub/js/custom-mycontrollername-mymethod.js you would then use (jquery);
$(document).ready(function() {
if ( $( "#mydivthing" ).length ) {
$('#mydivthing').html("fetching..");
$.ajax({
........
});
} else {
console.log("skipped js segment");
}
});
here the jquery only runs when the document is ready (as jquery recommends)
and you can also use if ( $( "#mydivthing" ).length ) { to check that your div is actually on the page. so you are not firing events when they are not loaded (for speed). but as jquery is likely doing some checking - this is probably only helpful for larger runs of code than just single button clicks events.
while the question was for views, this answers gets to a controller/methods level. so while a method can have multiple views - this still a view/screen as seen by the user.
added bonus that you can associate .js files to controller/methods just by its filename.
you can use a variable as a controller name and pass this variable to your view like this
Here is Controller
class Blog extends CI_Controller{
public function index(){
$data['controller']="blog_index";
$this->load->view('header',$data);
}
public function second_page(){
$data['controller']="blog_second_page";
$this->load->view('header',$data);
}
}
Header.php File
<? if (isset($controller) && in_array($controller,array('blog_index'))) : ?>
<script type="text/javascript" src="assets/js/custom_blog_index.js">
<? endif;?>
<? if (isset($controller) && in_array($controller,array('blog_second_page'))) : ?>
<script type="text/javascript" src="assets/js/custom_blog_second_page.js">
<? endif;?>
<!-- Other html code here -->
hope this will answer your question
I don't understand your problem. I suppose you load your js files in view site_header or view site_footer, why don't you load another view for these pages? for example:
public function index(){
$data['title'] = "Register";
$this->load->view("site_header_extra_js", $data);
$this->load->view("site_nav");
$this->load->view("content_register");
$this->load->view("site_footer_extra_js");
}
Another solution is to put an if in your view to load extra js files checking the path, you can use $this->uri->segment() for this.

Always redirected on 404 page with URL Rewrite and Codeigniter

So I download a clean copy of Codeigniter and replace the welcome controller and welcome_message view with (respectively) :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller
{
public function index()
{
$this->load->view('welcome_message');
}
public function test($number = 3)
{
echo $number;
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
and
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div>
LOLMDR
</div>
</body>
</html>
and my .htaccess file is :
RewriteEngine on
RewriteRule ^pouet/(.*)-([0-9]+)\.html$ index.php/welcome/test/$2
I thought that when I clicked the link I would be redirected to my welcome/test function and then echoing 2. But instead I have a 404 page and I don't understand why.
Thanks
add this to you routes
$route['test/(:num)'] = 'welcome/test/$1';
then go to url:
http://www.yoursite.com/test/3457
let us know the outcome dude
welcome is your default controller, so :
http://www.yoursite.com , will always = http://www.yoursite.com/welcome

Categories