how to use relative link in codeigniter? - html

I have the following code in Controller:
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('home');
}
function index()
{
$this->load->view('layout');
}
function about()
{
$this->load->view('about');
}
}
If I want to create a relative link to about, how do I accomplish that? The following link in view doesn't always work. What is apporpiate way to about relative links in CodeIngiter?
<li> About </li>

I believe that the reason your view code doesn't work is most likely due to not autoloading the URL Helper.
If you add the 'url' to the Autoloader (http://ellislab.com/codeigniter/user-guide/general/autoloader.html) the function site_url will become available on all controllers, views, and models.
File to edit: application / config / autoload.php
Line 110
Example of what it should look like (assuming you aren't auto-loading any other helpers):
$autoload['helper'] = array('url');

link should be name_of_controller/name_of_the_function
<li> About </li>

try this
<li> About </li>

Auto load your url helper and try
<li> About </li>

You want the anchor() function.
http://ellislab.com/codeigniter%20/user-guide/helpers/url_helper.html
<li><?php echo anchor('home/about', 'About') ?></li>
Use that in your view.
Updated

Related

How to keep current tabIndex when navigating between NextJS pages

How to properly save current tabIndex when navigating between pages?
(In ideas to make a function that will save the current index in the sessionStorage and retrieve it when the page is mounted)
Example:
a Simple solution based on the useRouter hook:
import Link from "next/link";
import { useRouter } from "next/router";
export const MyNav = () => {
const router = useRouter();
return (
<ul>
<li className={router.pathname == "/" ? "active" : ""}>
<Link href="/">API</Link>
</li>
<li className={router.pathname == "/users" ? "active" : ""}>
<Link href="/users">Users</Link>
</li>
</ul>
);
};
You can use withRouter() library to figure out current URL and match it with the page URL and then add a different class with border or background-color to differentiate between them
You can also go with a much easier method and use :active and :visited selectors of CSS with the nav-item class or element.

How to add css class to body tag in layout in yii2?

I want to add css class to body tag in yii2 advanced in frontend/views/layouts/main.php how can I do it?
You can do this dynamically like this:
<body class="<?= $this->context->bodyClass; ?>">
And in main Controller (all other controllers should extend this Controller) define property:
public $bodyClass;
or for default value:
public $bodyClass = 'custom-skin';
Ofc you can override this property in any extending controller by redefining it:
public $bodyClass = 'custom-skin-2';
In init:
public function init() {
parent::init();
$this->bodyClass = 'custom-skin-2';
}
In specific action:
public function actionView()
{
$this->bodyClass = 'custom-skin-3';
return $this->render('view');
}
You add your class simply to body tag
<body class="yourClass">
Another possible solution would be using the variable $params in your view.
Example
In your view you can define:
$this->params['bodyClass'] = 'yourclass';
And then, in your layout file, you'd go:
[.. head and other codes ..]
<body <? if(isset($this->params['bodyClass'])) echo 'class="' . $this->params['bodyClass'] . '"'; ?>>
<?php $this->beginBody() ?>
[.. rest of your code ..]
Notice that
I'm suggesting you to use the if so it only puts the class if it the $params['bodyClass'] is set in your view.
Also, you can use whatever name you want in the place of bodyClass.
This example will output <body class="yourclass">
Cheers.

How to link to a webpage that is in the same folder and level

I am having trouble with accessing a html page that is in the same folder directory and level. Every thing I tried results in a error on the page. I have tried a few ways to navigate to the page, but all fail except for main 5 it just opens the login page again so that does not work either.
I am a bit lost on how this works. I have attached a picture of now my structure is set up below.
I am trying to get from home.html to main.html
I am running Spring framework.
And this is the code that I am using to as the link.
{{>partials/header}}
<h2>Login Page</h2>
<h4>Links</h4>
<ul class="nav nav-pills nav-stacked">
<li role="presentation">
Main 1
Main 2
Main 3
Main 4
Main 5
</li>
</ul>
{{>partials/footer}}
Here is my controller
//New login page
#RequestMapping(value = HOME_URL_MAPPING)
public String inventory(final Model model) {
return controllerHelper.createUrl(INVENTORY, WebGlobals.HOME);
}
//new page Was the home page before
#RequestMapping(value ="/main")
public String inventgus(final Model model) {
UserDetails activeUser = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
model.addAttribute(CAN_MAKE_REQUEST, canMakeRequest(activeUser));
model.addAttribute(CAN_APPROVE_REQUEST, canApproveRequest(activeUser));
return controllerHelper.createUrl(INVENTORY, "main");
}
You were so close! Here's the answer:
Main
The single dot refers to the current folder and putting a slash means that the file is under that folder.
this works for me:
Main

LogOut not working properly

I have some trouble with logout on my page. i have a controller with this
public ActionResult LogOut()
{
FormsAuthentication.SignOut();
Session.Abandon();
return RedirectToAction("Index", "Home");
}
And so a view called LogOut. I've put an href on it with a button :
<li><a class="logout" href="~/Views/Account/LogOut.cshtml">Se déconnecter</a></li>
but when i click it keeps saying me your page couldn't been found etc...
But strange thing is this, if i put in my adress bar, the path to the LogOut view it works and i'm disconnected, someone knows why ?
You should give href like this instead of path to cshtml file:
<li><a class="logout" href="#Url.Action('Logout', 'ControllerName')">Se déconnecter</a></li>
it will hit LogOut Action Method then will Render your view Appropriately
You can set link in different way :
1) <li><a class="logout" href="/Account/LogOut">Se déconnecter</a></li>
2) <li><a class="logout" href="#Url.Action("LogOut","Account")">Se déconnecter</a></li>
3) <li>#Html.ActionLink("Se déconnecter", "Account", "Logout", new { #class="logout" })</li>

Twig and CodeIgniter form rendering issue. Form is being displayed as a String and not the HTML form

My first time to use Twig template with CodeIgniter. I'm used to the default form functionality of the framework, but I was asked to try to render the form using Twig. I find the template engine to be nice and confusing at the same time. So that means, my controller would be very fat with code. But the main issue here is to render the form using twig.
Below is what I used to do when I want to render a form. View: TableSample.php
<?php
echo form_open("", array("name"=>"form_reg", "method"=>"post", "id"=>"form_reg"));
echo form_input("type"=>"text", "name"=>"fname", "value"=>set_value("fname"));
echo form_input("type"=>"text", "name"=>"lname", "value"=>set_value("lname"));
echo form_input("type"=>"text", "name"=>"emailaddress", "value"=>set_value("emailaddress"));
echo form_input("type"=>"submit", "name"=>"submit", "value"=>"Submit");
echo form_close();
?>
Controller: register.php
public function register (){
$this->load->view("TableSample");
if($this->input->post("submit")) {
/** retrieve input details, pass them as array to model, then redirect if registration is successful**/
}
}
But since I have to use Twig, things have been a little bit different.
public function register () {
$detail["form_open"] = form_open("", array("name"=>"form_reg", "method"=>"post", "id"=>"form_reg"));
$detail["form_input_name"] = form_input("type"=>"text", "name"=>"fname");
$detail["form_input_lname"] = form_input("type"=>"text", "name"=>"lname");
$detail["form_input_eadd"] = form_input("type"=>"text", "name"=>"email");
$detail["form_input_submit"] = form_input("type"=>"submit", "name"=>"submit", "value"=>"Submit");
$detail["form_close"] = form_close();
//codes for saving here
//call twig view
$this->twig->display("tableSample.html.twig", $detail);
}
tableSample.html.twig would be like this:
<html>
<head></head>
<body>
{{ form_open }} //will display form as a **String** and not THE **HTML** like this:
<form method="post" name="form_reg" id="form_reg"></form>
{{ form_close }}
</body>
</html>
I know I'm missing something, please point me to the right way of rendering this. Thank You!
ok, I think I got it. raw made it possible. Twig Raw Filter