how to create function for load menu in codeigniter - codeigniter

i'm litte confuse about this, so i wanna make one function which called menu_function.
for what? so all file php in view, will load this menu_function
this menu_function will load menu option in each php file in view, so it will load different menu option in different php file in view
what i'm think is like this
public function menu_function()
{
$dataV["menu"] = "<a href='profit' style='color:blue; font-size: 14px; font-weight: bold;'>Profit PT</a>";
$dataV["menu1"] = "<a href='profit/member' style='color:blue; font-size: 14px; font-weight: bold;'>Profit Member</a>";
$this->load->view("test", $dataV);
}
i think is bad having html tag in controller or i already read this, according that is efficient i make another php in view just for menu option and load in another view?
why i want to make this? so in future if i wanna add another menu option or remove 1 or more menu option just change in 1 file, not having open all view and remove menu option 1 by 1.
maybe another suggest? thanks

What I would do is to create a templates folder inside the views directory, and put there view files that you may require from several controllers.
/application/views/templates
Let's name that file header.php and it would look like this
<a href='<?php echo site_url('profit'); ?>' style='color:blue; font-size: 14px; font-weight: bold;'>Profit PT</a>
<a href='<?php echo site_url('profit/member); ?>' style='color:blue; font-size: 14px; font-weight: bold;'>Profit Member</a>
Then, you could include this template in every controller you would want
$this->load->view('templates/header');
Also, for future reference, if you want a function to be available in different controllers you can:
Extend the CI_Controller core class.
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
/* define here all the functions you want */
}
Then you would create your controllers extending MY_Controller instead of CI_Controller whenever you would want to call the functions.
Use helpers, libraries and drivers.

Related

Change Font Size from Admin Panel Laravel

I want to change the frontend font size from the admin panel in Laravel. Suppose there is a
<h2 class="intro">Introduction to PHP...?? </h2>
in style.css-->>>>
.intro{
font-size:20px;
}
I want this font size dynamic from admin panel by catching the class. If i need 25px i use that or any size I need I will use that.
How can I do that in laravel??? Give me some suggestions to step up...
You can simply use something like this:
Save a file named style.php in the path where the asset files are located. Then use in your base blade file like this:
<link href="{{ asset('path/style.php') . '?fontSize=' . $fontSize }}" rel="stylesheet"/>
The fontSize variable is the font size you want to use.
And use in style.php
<?php
header("Content-Type:text/css");
?>
.intro {
font-size: <?= $_GET['fontSize'] ?>;
}
One way to do this would be via Javascript and your controller:
In your controller, just pass a variable called $fontSize to your view file. The with JavaScript, get the element by the class name and set the CSS font-size property to whatever value your $fontSize variable has like this:
<script>
let h2Item = document.querySelector('.intro');
h2Item.style.fontSize = '{{ $fontSize }}'
</script>
Remember that the variable you are passing from your controller should not just be an integer, rather it should be something like 20px 30px 2em etcetera.

Can I add a razer view file in my wwwroot folder and use my controller to point to that .cshtml file and pass my payload?

I have a working ASP.NET Core 5.0 MVC application working. I want to know how I can add a razor view file in my wwwroot folder and use my controller to point to that .cshtml file and pass my payload?
I'm wondering if I can adjust my controller class to point to a razor view page that resides in wwwroot? Right now I have my controller class set up like this:
[Route("~/wwwroot/helloWorld/HelloWorld.cshtml")]
public IActionResult Index(string pageId)
{
switch (pageId)
{
case "Foo":
DataModel foo = new DataModel(pageId);
return View(foo);
case "Bar":
DataModel bar = new DataModel(pageId);
return View(bar);
}
return View();
}
I have a hyperlink set up in my Views/Home/Index.cshtml like this:
<ul class="navbar-nav flex-grow-1">
<li class="nav-item" style="display: inline-flex; margin-right: .5em; vertical-align:top">
#Html.ActionLink("Home", "Index", "Home")
</li>
<li class="nav-item" style="display: inline-flex; margin-right: .5em; vertical-align:top">
#Html.ActionLink("To Hello World", "Index", "HelloWorld", new { id = "Foo" })
</li>
</ul>
But I get this error every time:
No webpage was found for the web address:
https://localhost:5001/wwwroot/helloWorld/HelloWorld.cshtml?id=Foo
Am I doing something wrong or is this just not possible? If it's not possible, I'd like to understand why. Many thanks in advance.
But I get this error every time:
No webpage was found for the web address:
https://localhost:5001/wwwroot/helloWorld/HelloWorld.cshtml?id=Foo
Am I doing something wrong or is this just not possible? If it's not
possible, I'd like to understand why. Many thanks in advance.
It is possible to return/redirect the html page in the wwwroot folder from the MVC controller, but it is impossible to transfer the data/model from the controller to the static html page.
Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients by default. When access them, it will go through the UseStaticFiles() middleware, instead of via the asp.net core routing.

Skeleton page with v-cloak: how to target loading div with css ? Vue

I'm using Webpack/Laravel and injecting vuejs on id #app in my page so to have skeleton loading page I want to have this markup
//client side, will show after ~0.2 seconds
<div id="app" v-cloak>
<hello-world>
</div
//server side, show for ~0.2 s then disappear
<div id="app__loading" v-cloak>
<div class="skeleton">
<span class="skeleton_ribs"></span>
</div>
</div>
I managed to display loading gif in background as pseudo element when v-cloak and everything inside from #app__loading is removed, but if I add normal elements in markup they appear at the bottom of the page after #app loads
[v-cloak] > * { display:none }
[v-cloak] + #app__loading::before {
content: url('https://i.pinimg.com/originals/65/ba/48/65ba488626025cff82f091336fbf94bb.gif');
display: flex;
justify-content: center;
align-items: center;
}
But I would like something like this to work with markup inside #app__loading, but it doesn't work
[v-cloak] > * { display:none }
#app-loader{
display: block;
}
[v-cloak] #app::finish-loading{
[v-cloak] #app-loader{
display: none!important;
}
}
You seem to be expecting the content your initial element to be found in the template of app root element. When using $mount() function, Vue completely replaces the target element with the template of the mounted element.
This replacement is somewhat masked by the fact that, out of the box, the index.html contains a <div id="app"> which gets replaced by the <div id="app">...</div> in your App.vue template.
But they're actually not related (and not the same element). If you changed the id of the one in index.html you'd need to change the target el in main.(js|ts). And obviously, you could change the id of the one in App.vue as well.
Now, to solve your issue, you could simply place v-cloak on the div in your App.vue. From what you've shown so far, that should make it work as expected.
For more complex use cases (i.e: if you wanted to trigger a particular event bound to the initial element) the way to go would be to wrap the initial placeholder in a parent, bind to the parent and, later on, in Vue, target the parent with this.$el.closest('.some-class') or with this.$el.parentElement() to access the binding.
If you still can't get the expected result, please create a mcve (you could use codesanbox.io) and describe in detail what is the expected behavior.
To make your headache smaller till you find proper vue solution, you can grab loader by id and when Vue instance mounts - give display none
export default {
mounted() {
document.querySelector('#app__loading').style.display = 'none';
},
}

How to specify background image with Thymeleaf?

I'm learning spring boot with thymeleaf. I've stored user object's profile image in the file system and only the URL of the image is part of the User object persisted in the database.
I've added the name and dpurl into the model, in order to access it from the template.
#GetMapping("/profile")
public String profileController(#AuthenticationPrincipal User activeUser, Model model) {
model.addAttribute("name", activeUser.getName());
model.addAttribute("dpurl", activeUser.getDpURL());
return "profile";
}
I'm able to access name from template using th:text="${name}"
However, I'm not able to specify background image using thymeleaf like this
<div id="dp-container" th:style="'background-image: url(' + #̣{dpurl} + ');'" >
this is resulting in error Could not parse as expression: "'background-image: url(' + #̣{dpurl} + ');'"
Update
Changed it like this
<div id="dp-container" th:style="'background-image:url(' + #{dpurl} + ');'">
Now I'm not getting any error but background image still not being displayed.How to specify background-image using Thymeleaf?
It probably needs to look like this:
<div id="dp-container" th:style="'background-image:url(' + ${dpurl} + ');'">
But it depends on what dpurl contains. Do you really need to use #{...}? Then you should use #{${dpurl}}.
To debug this, you should be looking at the source. it will reveal what your expressions are resolving too.
I got this to work:
<td th:style="'background: url(/images/aws2.png)'" class="fluid hero-image" >
Well, if you've got something more complex like needing to pass a variable into the url, you can try this:
<div id="profile_image" th:style="'background-image: url('+ #{'/view-profile-pic/' + ${result.profile_pic}} +');'"></div>
And in css, the image was displayed correctly with:
#profile_image {
margin: auto;
height: 200px;
width: 200px;
box-sizing: border-box;
border-radius: 100px;
background-size: cover;
}
I stumbled upon this solution when I wanted to display profile pictures received from search results.
You could try this too if none of the others work. Upload the image to some online store like a file sharing service or google drive and provide the link to the image as the url for your background-image property.
<div id="dp-container" style="background-image: url(Link to the image);" >

CodeIgniter - Including sections of code?

I have about 9 lines of code that will be the same in many of my controllers. They go in the index and are used to check whether the user is logged in, what level of access has been granted, and grab some session variables. What's the best way to include snippets of code that are not complete functions within themselves?
The snippets are not in all controllers, just the ones that build the admin section of the application.
It would be great if I could use one line of code to include the snippet but I'm new to CodeIgniter and I don't want to stray from best practices. If you could include a brief example that would help me visualize this. Thanks!
You want to create an extension of the base CI controller and do your checks there. Extending the cores are in the documentation here: http://ellislab.com/codeigniter/user_guide/general/creating_libraries.html Scroll down to the Extending Native Libraries part.
In your new controller you can do something like this:
<?PHP
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct(); //Gets all the CI_Controller functions and makes them available to this controller.
if($this->session->userdata('is_logged_in')
{
$this->load->model('categories');
$this->categories=$this->categories->getAllCategories();
}
}
}
Then in your other controllers you use MY_Controller instead of CI_Controller like this:
<?php
class Pages extends MY_Controller {
Obviously you'll have your own checks and variables to load but this should give you the general idea. In the example above I can now use $this->categories to retrieve my categories anywhere in the application that's been loaded with MY_Controller.
I'm using a 'main' template file (views/template.php):
// views/template.php
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view('includes/' . $main_content); ?>
<?php $this->load->view('includes/sidebar'); ?>
<?php $this->load->view('includes/footer'); ?>
So the view directory structure is something like this:
--views
--includes
- header.php
- footer.php
- template.php
As you can see inside the view/includes/ folder I'm using some reuseable snippets like 'header.php', 'footer.php'...
So inside a controller I'm loading this main template file:
class MyController extends CI_Controller {
public function index()
{
//...
$this->load->view('template',$data);
}
}
So it loads 'template.php'.
And can use the $data inside header.php, footer.php too..
But you can vary this as you want, it's just a 'basic' idea..

Resources