In my Laravel 4 application's root directory, I have a folder themes. Inside the themes folder, I have default and azure.
How can I access view from this themes/default folder in a specific route.
Route::get('{slug}', function($slug) {
// make view from themes/default here
});
My directory structure:
-app
--themes
---default
---azure
I need to load views from localhost/laravel/app/themes/default folder. Please explain this.
This is entirely possible with Laravel 4. What you're after is actually the view environment.
You can register namespace hints or just extra locations that the finder will cascade too. Take a look here
You'd add a location like so:
View::addLocation('/path/to/your/views');
It might be easier if you namespace them though, just in case you have conflicting file names as your path is appended to the array so it will only cascade so far until it finds an appropriate match. Namespaced views are loaded with the double colon syntax.
View::addNamespace('theme', '/path/to/themes/views');
return View::make('theme::view.name');
You can also give addNamespace an array of view paths instead of a single path.
Here I am not accessing my project from public folder. Instead of this I am accessing from project root itself.
I have seen a forum discussion about Using alternative path for views here. But I am little confused about this.The discussed solution was,
You'd add a location like,
View::addLocation('/path/to/your/views');
Then add namespace for theme,
View::addNamespace('theme', '/path/to/themes/views');
Then render it,
return View::make('theme::view.name');
What will be the value for /path/to/ ?
Can I use the same project in different operating system without changing the path?
Yes, we can do this using the following,
Put the following in app/start/global.php
View::addLocation(app('path').'/themes/default');
View::addNamespace('theme', app('path').'/themes/default');
Then call view like the default way,
return View::make('page');
This will render page.php or page.blade.php file from project_directory/app/themes/defualt folder.
I've developed a theme package for laravel 5 with features like:
Views & Asset seperation in theme folders
Theme inheritence: Extend any theme and create Theme hierarcies
Try it here: igaster/laravel-theme
\View::addLocation($directory); works fine but the new right way to do it is using loadViewsFrom($path, $namespace) (available on any service provider).
Related
Laravel 5.7.
If I navigate to a page that does not exist, I get 404 error page handling.
That view is located in vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Views/404.blade.php
However this file extends:
#extends('errors::illustrated-layout')
This is located in the same folder, and is named illustrated-layout.blade.php
So I guess that the errors:: part points to the specific folder., e.g. vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Views/
Question: Is this type of pointer something that could be created manually, so a person wouldn't have to write the entire path to a specific folder, when extending a view? Would make things much more clean.
You can add a view namespace and achieve the same result.
For example, you can add the following in AppServiceProvider#boot:
$this->app['view']->addNamespace('admin', base_path() . '/resources/views/admin');
and let's suppose you have a blade file in resources/views/admin/layouts/master.blade.php
you can access it with admin::layouts.master
I am working on a Laravel project and I am very new to it. For now, I want to use blade templates to render views but I want it to search for views in different directories like <custom_dir>\views instead of default resources/views.
The <custom_dir> will be dynamic (it can be a variable).
Any ideas? I was thinking of a custom service provider and then extend the default function which renders views in Laravel inside it. But not sure how to implement it.
Edit:
I have user this link to extend the default functionality of include function in blade template engine. But this overrides the include functionality. I want to change the path and then call the default blade functionality
You could probably append the path to the configuration:
1) Statically, by modifying file config/view.php
'paths' => [
realpath(base_path('resources/views')),
//more paths here
],
2) Dynamically at runtime:
$paths = config('view.paths');
$paths[] = $newPathToAdd;
config(["view.paths" => $paths ]);
I suggest you use this in moderation otherwise you will just end up with a mess of directories with no real specified purpose.
You can create custom directories in resources\views directory and use them with something like this:
return view($customDirectory.'.index');
Where index is a template inside custom directory.
I am a Drupal dev and new to code-igniter or any such php frameworks.
Now i have to modify an existing application done on codeigniter and the structure must be as follows:
example.com/motors
example.com/motors/car-for-sale
example.com/motors/car-for-rent etc.
Before it has only one url example.com/motors and i want to create more urls as mentioned above.
In the application\views\content folder i have the following structure:
application\views\content\motors.php
application\views\content\motors
application\views\content\motors\car-for-sale.php
In the application\controller folder i have the following structure:
application\controller\motors.php
application\controller\motors\motors.php
application\controller\motors\car-for-sale.php
I want to get the url example.com/motors & example.com/motors/car-for-sale from the files resides in the motors folder.Also how can i set a default file to load when i open example.com/motors?
You can't have a (controllers) directory that matches the name of a controller class at the same level. That is, since you have a controllers/motors.php, the files under controllers/motors/* will never be reached.
Instead (and this is the answer to your second question), you should set the default_controller name and rename controllers/motors.php to controllers/motors/<default_controller>.php.
Note that the default_controller setting points to a controller name (not a file location) and is applied to all directories. That is, if you set it to 'Default', then controllers/Default.php will be used when you open http://domain.tld/ and controllers/motors/Default.php will be used if you open http://domain.tld/motors/.
Also, your controller names MUST start with a capital letter, so default.php would be incorrect and should be Default.php instead. This might be working for you on Windows right now (because of its case-insensitive file system), but as soon as you upload your site to a Linux (or other UNIX-based) host, any classes with file names that don't start with a capital letter won't work.
It looks like you're trying to build a CodeIgniter site with a completely different paradigm from what it is designed around.
The structure you are after can be set up using the routes.php file within application/config
In there, you can set routes to go to any location needed, so for you, something like:
$routes['motors/cars-for-sale'] => 'motors/cars_for_sale';
$routes['motors/cars-for-rent'] => 'motors/cars_for_rent';
Then in application/controller you'd have a Motors.php file, which starts:
class Motors extends CI_Controller{
And also has the functions cars_for_sale and cars_for_rent
The mappings in routes sets this to link together.
In order to get the views you want for any given route, in the controller function, you'd have:
$this->load->view('path/to/view/file', $array_of_data); // view path does not need the .php extension
I'd recommend having a look and possibly even a follow through of the CodeIgniter tutorial in their documentation
I am new to yii framework. On yii version:2.0.1 I have created a module in which I tried to generate a CRUD model using the gii functionality. After putting the model class, controller class and view path when I clicked on generate gii showed all the files has been created successfully. But when I tried to view, below message has been shown to me,
The view file does not exist :
C:\xampp\htdocs\advanced\backend\modules\settings\views\companies\index.php
I found the view files in web directory not in the path I entered. Hence the error.
Here is my inputs to gii,
Model Class:
backend\modules\settings\models\Companies
Search Model Class:
backend\modules\settings\models\CompaniesSearch
Controller Class:
backend\modules\settings\controllers\CompaniesController
View Path:
backend\modules\settings\views\companies
When I click generate I have given below message :
Generating code using template "C:\xampp\htdocs\advanced\vendor\yiisoft\yii2-gii\generators\crud/default"...
generated modules\settings\controllers\CompaniesController.php
generated modules\settings\models\CompaniesSearch.php
generated backend\modules\settings\views\companies\_form.php
generated backend\modules\settings\views\companies\_search.php
generated backend\modules\settings\views\companies\create.php
generated backend\modules\settings\views\companies\index.php
generated backend\modules\settings\views\companies\update.php
generated backend\modules\settings\views\companies\view.php
done!
Does anybody have any idea why is it happening.
Thanks in advance.
I solved similar problem by changing
backend\modules\settings\views\companies
into
#backend/modules/settings/views/companies
Hopefully it helps someone in future
You missed one final folder and alias in View Path. It should be #backend\modules\settings\views\companies.
As you can see in creation log, the generated files are in wrong place (root views folder), that's why the error is thrown.
Have the same problem. Solved entering #backend/modules/settings/views/companies instead backend\modules\settings\views\companies.
See on slashes.
For view path in CRUD generator, enter the absolute or full path. For example
/home/developer/workspace/advanced/backend/views/<your view folder>
put this in the VIEW PATH
#backend/modules/settings/views/companies
and it's done!
I have tried with absolute path i,e
C:/xampp/htdocs/advanced/backend/modules/settings/views/companies
It worked for the absolute path.
I tried this is working by entering:
/Applications/XAMPP/htdocs/advanced/backend/modules/settings/views/companies
I hope you can do it well.
I'm using the advanced theme
Here is my Gii setup in case it is helpful for anyone
module generator
----------------
module class: backend\modules\posts\Module
module id: posts
model generator
---------------
table name: posts
model class: Posts
namespace: backend\modules\posts\models
Enable I18n: checked. category: app
CRUD generator
--------------
Model class: backend\modules\posts\models\Posts
Search model class: backend\modules\posts\models\PostsSearch
Controller class: backend\modules\posts\controllers\PostsController
View path: #backend/modules/posts/views/posts
Enable I18n: checked. category: app
Enable pjax: checked
If you are using some non-default user management (like amnah module),
you need to change Users::className() in the models\Posts.php to
\amnah\yii2\user\models\User::className()
Previously I put a model into the incorrect directory when wanting to achieve an absolute path (address it absolutely as it seems to be more often functional) -
app/backend/modules/settings .
Notice the 'app' in the beginning (the /app is the main directory in Yii starter kit) which I thought would avoid the relative path but must NOT be there nor #mail sign, but on the contrary, it did the opposite - it appended it relatively with its absolute length to the /document root directory, basically duplicated it,
"app/myproject.com/app/backend/modules/settings"
So it unwinds from the model location, the causes of Gii complaints about incorrect paths or put also controllers the incorrect way.
Also strangely enough for views in contrast to models, it had to be put the upper mentioned different way with the # relative reference sign annotation (which was not allowed to be used for controllers or models at the time of writing)
#backend/modules/settings/views/companies, otherwise it appended the directory tree into the document root, again concatenated - the web directory of backend backend/web (backend/modules/settings/views/companies)
I have two versions of my project. For one i use a different CSS and index page and for another i use different. Rest of the things that is controller, models and components are same. The only difference is in view(one or two files) and CSS.
Is there any way to manage this? Like when the URL is URL1 then use CSS1/View1 folder and when url is URL# use CSS2/view2 folder. I have gone through the modules section of Yii but i don't think they are what i need here.
So now I started to use themes. My folder structure is like:
WebRoot
- assests
- css
- images
- protected
- themes
- theme1
-views
-site
-layout
-template
- theme1
-theme2
-views
-site
-layout
-template
In my controller I have done this:
public function init() {
if (SITE_TITLE == 'xxxxx')
Yii::app()->theme = 'theme1';
else
Yii::app()->theme = 'theme2';
parent::init();
}
Which sets theme correctly. but i keep getting file not found as renderer is looking in protected.
I think, you need use themes. Here is documentation: http://www.yiiframework.com/doc/guide/1.1/en/topics.theming
UPDATED after discussion
Trouble in ETwigViewRenderer and it working with themes
If you want to change entire layout, perhaps this is a good way to do:
Setting Layout in Yii
In case you want only to change css they why don't you rely on request uri or domain name?
Yii::app()->getBaseUrl(true)