Get location of view passed into laravel view composer - php

Is it possible to get the location of a view that is passed into a Laravel view composer?
View::composer('*', function($view) {
// I want to find out the location of the view file here
// e.g. master.something.header
// then add this to an array
$loadedViews = View::share("loadedViews");
$loadedViews[] = $thisViewName;
});
The reason is that I want to have a variable that will be shared between views and contain an array of all the views that are loaded. Any css and js files will be located in directory structure that matches the views one.
This means I can then have a css and js view which then include the required css and js files for the views on the page. All css and js will be directly linked to a specific view.
If there is already a way to do this, or a way to get a list of loaded views, please let me know!

$view->getName() was the answer that I wanted.
$view->getPath() is the actual path to the file.

Related

Moving stylesheets to the document head during PHP execution

I'm trying to come up with a system where visual components like foo or faa would be stored in the /components folder, and each component would be in its folder with that components files, say /foo, and the component files foo.component.css and foo.component.php inside it.
The name.component.php has some HTML and a style <link> inside, referring to the name.component.css. which styles that component. Components are included in page files, such as index.php, which gets its <head> tag from head.php, which is outside the root.
The file hierarchy would look like this:
├──* head.php
└──* /root
├──* index.php
└──* /components
├──* /foo
│ ├── foo.component.css
│ └── foo.component.php
└──* /faa
├── faa.component.css
└── faa.component.php
When index.php includes a component, its CSS will be added outside the <head>, which I would like to avoid. Is there a way to move the CSS link to the document <head> during the PHP execution, for example, with a custom function? The CSS needs to be moved from the name.component.php specifically, so manually adding the CSS to the head.php won't do.
File: head.php
<head>
<!-- Other non-component stylesheets here; -->
<!-- Component stylesheets would be moved here during PHP execution; -->
</head>
<body>
File: index.php
require_once("../head.php");
require_once("coponents/foo.component.php");
File: foo.component.php
// Can this be moved to the head during execution from this folder?
echo('<link href="/components/foo/foo.component.css" rel="stylesheet">');
// Some HTML elements here...
// Trigger something here that moves the CSS link to the head.php
Could buffering be an option here? Any pointers would be appreciated.
Should your component really define the css with an echo?
Your index.php could insert it, if you followed a naming convention. The problem you'll see is when you have more complexe things to do...
The way I'd do it is to create some sort of manifest for your component. You'd have a class that would list the required css files, javascript files (why not) and template files. Your index.php could easily run through the definition and include at the proper places.
// File foo.manifest.php
class FooComponent implements Component{
public $stylesheets = ['foo.component.css'];
public $javascripts= ['foo.component.js'];
public $dependsOn = []; // You could set dependencies here so other components are loaded if needed.
public $template = 'foo.component.php';
}
You index would load up the class, and loop through its stylesheets to echo them at the right place.
$components = [new Foo(),new Faa()];
foreach($components as $component){
foreach($component->stylesheet as $stylesheet){
echo ('<link href="'.$stylesheet.'" rel="stylesheet">');
}
}
require_once("../head.php");
foreach($components as $component){
require_once($component->template);
}
You'll have to figure out how to play with the paths tough, either your manifest declares them relative to the index.php or you find a way to know the file from where the manifest class comes from so you can make a relative path from it, unfortunately I'm not very good at PHP for this...
What I am doing in the website I am working on now is to have a single head.php file which I bring into the PHP files for the various pages on my site. Sometimes a particular PHP file for one of the pages may require a different JS file or CSS file. So, I have <?= $headEls ?> inside the head element in head.php.
Now, inside one of the PHP files for the pages I can assign a value to $headEls which is just a string for any script or link elements that may be required.
One problem is that with the above system you have to assign a value to $headEls inside every PHP file (even if you just make it an empty string) or you get an error trying to access an undefined variable. In order to get round that I put the following line inside head.php before trying to access $headEls: The line is $headEls = isset($headEls) ? $headEls : '';. So now I only have to assign a value to $headEls if I actually have a CSS or JS file(s) I want to bring into it.
I am sure that better systems can be thought of but it does work and I do not want to use a framework which solves all these problems plus ones I have never thought about but involves 1000s of files on my server and my local machine.

Paths to differents files in one view - Joomla

I'm trying to put a link in a component's view file (default.php) to another file in the same view (default_formulaire.php) as you can see below:
But I really don't know how to access it in PHP.
I know that the default.php file's url is:
index.php?option=com_multicontact&view=reclamation
but I don't know that of default_formulaire.php.
Thanks for any help :)
The link to any view in Joomla is the following:
index.php?option=com_componentname&view=viewname&layout=layoutname
However, if the layout is omitted from the URL, then it is assumed that it is set to default. So, the following URL:
index.php?option=com_componentname&view=viewname
Will mean that the layout is default, which means that the default.php file will be loaded.
So, in your situation, the URL to load the default_formulaire layout will be:
index.php?option=com_multicontact&view=reclamation&view=default_formulaire
If you need to access a different layout in joomla then you need to add a layout value in joomla url like index.php?option=com_multicontact&view=reclamation&layout=default_formulaire

How to automatically include/load css and js from modules in CodeIgniter using HMVC

I have some modules in modules folder that have css and js folder and I am thinking of the way how to approach the automatic load of all this css and js files to my header of the template.
I was thinking of creating 2 modules called cssloader and jsloader that will be included in the header section of the template.
They would contain some php script that will put the urls of css (and js) in an array and this array will be outputed in the template like e.g.:
<?php echo Modules::run( 'cssloader/cssloader/_css_include_for_frontend' ); ?>
The urls will be grabed by some script that will be searching modules folder of the CI application and looking for css folder within and a file load_css.php with some defined constants or variables like
$css_loader_frontend['slider'] = array('slider.css',
'slider_ie6.css'
);
$css_loader_backend['slider'] = array('slider_admin.css');
This file will contain files that will be loaded e.g. slider.css (within slider module css folder)
And the similar scenario for javascript stuff.
Is my approach right or not and you would do it somehow different?
What do you think about it?
What would you do different and more effective?
Thanks
I Think this template class can help you, though, I was wondering why are you accessing assets files inside modules, I'm not well versed but as far as I know you should access files such as img, js, css and so on just on the level of system and application folders in a folder that could be named "public" or "assets" to avoid "Directory access is forbidden."

how to add stylesheet to my layout and pass values from view to layout

I am using zend framework.
My structure is (only included files and folders needed for this question):
application
>configs
>controllers
>forms
>images
>layouts
>scripts
>layout.phtml
>models
>styles
>style.css
>views
>scripts
>index
>index.phtml
Bootstrap.php
docs
library
logs
public
test
I have got the layout working properly. However, I want to ask a couple of questions in order to get my set up perfect for the way I want it.
Is application>styles a good place for the stylesheet to be? If not what's the recommended?
How do i add the stylesheet to the layout?
In my layout I have a title tag : <title>Text</title>. How do I pass values from my controllers to it?
Stylesheets need to be accessible from the browser, so typically you will put these somewhere in the public directory, such as public/css
There are several ways, including placing rel tags in your view/layout, but my preferred option is to use the viewHelper within your controller:
$this->view->headLink()->setStylesheet('/css/style.css');
Then your call to headLink() in the layout file will automatically include the stylesheet.
The way I have done this is to use the Zend_Registry in the past. There may be better ways.

How to dynamically control paths in static CSS files with MVC code igniter project

I'm creating a website/codeigniter-project that uses views which link to external CSS files.
Everywhere throughout my project/web-page's views I can control the URL paths of images, links, etc by constructing them from the 'base_url' setting variable. I don't have any control over static, linked external CSS files. This means that whenever my base URL path changes for my site I have to go through my CSS files and do global search/replaces to update all my paths.
To solve this I thought about creating a controller just to load CSS/JavaScript files and treating the actual files like views with hooks but I was talked out of this by #WesleyMurch in this question:
Using a controller to handle returning customized css & javascript files with codeigniter
How can I dynamically assign base-paths to my css assets so I don't have to do global search and replaces every time I update the base path of my site?
For this I use a view file containing css code with all the variables. The only change is that you should set appropriate headers for CSS
Create function style in your controller and set it to render appropriate view file (style.php). Code all your css with php code in style.php.
Set following headers at the start of the function:
header("ContentType: text/css");
header("Expires: <some far future expiration time or use mod_expires with apache>");

Categories