Laravel #extends() pointer system - php

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

Related

how to duplicate a page in codeigniter?

I'm new to codeigniter environment. I have this existing system and I am trying to duplicate one of the admin pages which I thought would work if i just copy, rename and paste.
Example:
(original files and working)
application\controllers\admin\calculator.php
application\views\admin\calculator.php
it can be access by domain.org/admin/calculator
(duplicated)
application\controllers\admin\calculatorduplicate.php
application\views\admin\calculatorsduplicate.php
tried accessing by domain.org/admin/calculatorduplicate
but showing 404
I also added the navigation link inside this file.
application\views\admin\header.php
What am i missing? I didn't want to change anything inside the file yet, just want to duplicate the page. Please help.
Firstly, first try to understand how CodeIgniter binds its URLs.
In URL:
http://codeignitersite.com/controller/method/parameter_one/parameter_two/[parameters].....
Controller is the Controller class which must have the same name as of its Filename and also as in URL.
Method is the Controller method to be accessed.
In above case, the class name also needs to be changed to:
class Calculatorduplicate {
To avoid any 404 error.

Create folder/Sub-folder in Code-igniter

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

Yii 2 gii is generating view files on different directory

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)

How to load view from alternative directory in Laravel 4

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).

Easy way to find the controller file with only the URL on Cake PHP

Being new to Cake on PHP, I am trying to work out if I have a URL, what would be the easiest way to find the controller code for it?
The URL on my local machine is something like:
http://foofoofoo.local/protected/admin/org/edit/1
I have worked out that the location of the view for this file is at this location on my machine:
/var/www/MyApp/protected/app/views/org/admin_edit.ctp
I thought what I'd do is do a search throughout the entire codebase for anything referencing admin_edit.ctp. I found two entries, and changed them to see if I had found the point where the view is called, but despite changing the file name on these entries - the app still works when I visit the URL: http://foofoofoo.local/protected/admin/org/edit/1
I just want to see where the admin_edit.ctp file is being called within the site.
URL: http://foofoofoo.local/protected/admin/org/edit/1
This means I can assume you have a added a route in your /app/Config/routes.php. Where this is pointing can not be said since we don't have access to this file.
Why can I assume you have added this to your routes? Because the posted URL is not matching the CakePHP Conventions which clearly states that controllers should be defined in plural. Since the URL will be accessing the Controller directly through the Controller, unless a route has been specified, I know that the OrgController does not exist. Why?
Try Inflector::pluralize('Org'). It will return 'Orgs' to you. And thus meaning the controller should be called OrgsController and you should be accessing this Controller via the following URL.
http://foofoofoo.local/protected/admin/orgs/edit/1
In this OrgsController there should be an action (function) called admin_edit(), because you have prepended the org with Admin, which is a prefix.
It can be possible that the /protected part, is part of the URL as well, but do not know where your main /App is located and what part of the URL is pointing to the /app/webroot/index.php file.
The Views can be found at /app/View/Orgs/*.ctp.
If you are still having trouble finding your files. Please start with the Blog tutorial written by the Cake Community. This tutorial describes all the neat built-in tricks and will get your first app running in no-time. Please read that first!
If you are still having trouble, feel free to update your question and add the /app/Config/routes.php file.
Under Cake 1.3, if your application has an AppController (check if the file app/app_controller.php exists), you can put this code in the beforeFilter method:
debug($this->params);
It will print an array on your app pages when you are in debug mode, with the name of the controller and the action used.
Array
(
...
[controller] => controller_name
[action] => action_name
...
)
If the AppController does not contain any beforeFilter method, you can just create it:
function beforeFilter()
{
debug($this->params);
}

Categories