I'm on the way of creating a Laravel package for our internal use, that's why it's not in packagist, and not in vendor/ directory. Instead it's in packages/ directory - a custom directory of ours - a similar approach we used for another package of our internal use.
Issue is, in the blades from the package we used action('MyPackageController#method') to produce links. After publishing the view files to the application the current path of the views are like resources/views/vendor/mypackage/one-directory/index.blade.php.
With a global access to the controller it simply is working:
action('\MyVendor\MyPackage\App\Controllers\MyPackageController#edit', ['id' => $item->id])
But issue is, when I'm trying to impose aliases for the controller[s], they are not working. In config/app.php aliases, I mentioned:
'XYZ' => MyVendor\MyPackage\App\Controllers\MyPackageController::class,
'ABC' => MyVendor\MyPackage\App\Controllers\MyPackageSecondController::class,
and in index.blade.php I tried using:
action('ABC#edit', ['id' => $item->id])
but it's not working. Because it's trying to find the controller in App\Http\Controllers\:
ErrorException (E_ERROR)
Action App\Http\Controllers\ABC#edit not defined. (View: D:\laragon\www\test-laravel\resources\views\vendor\mypackage\one-directory\index.blade.php)
Aren't package controllers be aliased?
PS. I tried clearing all types of caching.
As even after a long time, Martin Bean did not add any answer to the thread, here I'm posting one for future record:
As Martin Bean said in his comment:
You’d used a named route instead of action(): laravel.com/docs/master/routing#named-routes
What I found is: the action() function has its own caveat, it cannot take other namespaces other than App\Http\Controllers. So named route did it for me: route('my_name', ['id' => $item->id]);.
Thanks to Martin Bean for his clue.
First of all, you have to register your routes in the service provider of your package.
boot() {
require __DIR__ . '/Http/routes.php';
}
__DIR__ would point to the root of your package folder. This is assuming your routes are registered in your packages' Http directory. If this is done, make sure you use a unique name for your route and just call your action like so:
action="{{route-name}}"
That's all you have to do.
Related
I’m quite new to Laravel, so patience with me.
I’m in the middle of the process of replicating a framework that I developed in node / react to laravel. Right now, I’m adjusting the main architecture and currently working on a blade master page.
Goal 1:
I want to load images using the blade {{ asset() }} helper in a template page. The thing is that I want to load the images from a subfolder that resides inside the resources folder:
/resources/app_files_layout
I know that I could do this by moving the folder to the public directory, but that’s not what I have in mind. My intention is also to advance on a deeper learning and understanding on the laravel framework and try to maintain an architectural fidelity with my framework on other languages.
Goal 2:
After I manage to load the images from the /resources/app_files_layout through {{ asset() }} helper in blade, I’d like to use an alias for the route in the source code. Example:
Instead of displaying in the output HTML in the browser http://localhost/app_files_layout/image-name.jpg, I’d like it to display: http://localhost/images/image-name.jpg and never show the user that the files are in a app_files_layout folder.
Is what I’m trying to do possible at all in laravel?
Worst case scenario:
If laravel doesn’t have the necessary architecture to do what I want, I could settle for setting up a route that displays the image in the /resources/app_files_layout folder. That is: when I access on the browser http://localhost/images/image-name.jpg should load an image located in /resources/app_files_layout/image-name.jpg.
I tried to follow this stackoverflow suggestion:
https://stackoverflow.com/a/38736973/2510785
However, returned me an error like so:
The requested resource /files-layout-test/backend-layout-header-tb02-03.jpg was not found on this server.
Thanks, in advance!
I managed to do what I wanted for this one. All credit for #donkarnash on this question that I posted for the worst case scenario for creating a route alias for it: Laravel 8 – create route alias for image folder inside public directory
I used the symbolic link approach, almost similar to what #sergey-ligus had mentioned in the comments.
1 – edit laravel \config\filesystems.php file:
Originally, looks like this:
links' => [
public_path('storage') => storage_path('app/public'),
],
After I edited, looked like this:
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('files-layout') => resource_path('app_files_layout'),
],
2 - Then, in the command terminal, I executed the following command:
php artisan storage:link
3 - Finally, I ran the server and tested out. Not only could I access the file in the browser via: http://localhost:8000/files-layout/backend-layout-header-tb02-03.jpg, but also could still use laravel asset blade helper.
Blade file:
{{ asset('/files-layout/backend-layout-header-tb02-03.jpg') }}
For my Laravel 5.5 project I used filemanager package (elfinder-laravel ) with published and adapted blade views. After awhile I found that default views are used from package folder:
/vendor/barryvdh/laravel-elfinder/resources/views
instead of published views:
/resources/views/vendor/elfinder
I tried to republish views, clear views and cache. But nothing helps, it still uses default package views.
Views in /resources/views/vendor/elfinder exist.
Any idea how to make it work?
Try to edit the service provider file to be like that :
public function boot()
{
$this->loadViewsFrom($this->app->resourcePath('views/vendor/elfinder'), 'elfinder');
}
It is forces the file to use this directory as main view directory instead of changing it.
The question is quite old, but you have to set in the
$this->loadViewsFrom(__DIR__.'/../resources/views', 'courier');
the same package name as in the
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/courier'),
]);
(notice both times the courier)
See: https://laravel.com/docs/8.x/packages#overriding-package-views
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).
I'm pretty new to Laravel, I've spent some time reading about it and doing some tutorials. Lately, I've been following this tutorial about creating an authentication bundle:
http://net.tutsplus.com/tutorials/php/build-your-first-admin-bundle-for-laravel/
Basically, it's creating a simple custom auth driver extending the default auth one. Everything works quite nicely.. inside the bundle. My problem is more about how to use/access this admin/login bundle in my main application. I feel a bit ashamed asking this, I guess it has something to do with loading/starting the admin bundle in my application controller(s), but i can't get it to work.
Thank you
You have a couple of options, you can either start the bundle manually from within your application controllers each time by calling:
Bundle::start("<Your Bundle Name>");
Or when you register the bundle with Laravel (when you add it to /application/bundles.php) you can also choose to autoload it:
return array(
// ... other bundles
"<Your Bundle Name>" => array("auto" => true),
);
From looking at the tutorial this might look something like:
'admin' => array('handles' => 'admin', 'auto' => true)
Once you have either started the bundle manually, or autoloaded it, you can then call the bundle classes directly (make sure you use the proper namespace when calling the class).
You can also check out Laravel's documentation.
Starting from the skeleton application using beta3 how would you resolve the view path for a new module called Foo?
I have added below to the di config and now both modules action's render Foo's views.
'Zend\View\Resolver\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'foo' => __DIR__ . '/../view',
),
),
),
I would expect Application\Controller\IndexController::indexAction() to render the views in Application and for Foo\Controller\IndexController::indexAction() to render Foo's views.
Note that questions like this help shape the direction of the stable framework. :)
One idea I've been toying with is to use the module as part of the view script resolution. Right now, the default used is "/"; my proposal is to use "//", as this would help prevent naming conflicts between modules; it also makes it much simpler to understand exactly what view script you are overriding if you use template maps.
You can use this approach today, but it will require manually setting the template on the view models you return from your controllers.
This doesn't currently work in ZF2 as there is no concept of taking the namespace into account when resolving view scripts. Discussions are currently ongoing on how best to tackle this.
For the time being, you have to name each controller differently. In general, we are recommending that you name the "primary" controller within a module after the module name. That is, the primary controller in the Foo module would be FooController.
You actually can do this; and it is not too bad....
Rob Allen himself had a blog post that basically makes this work... Notice you have to basically handle it as a module based loader that separates much of the work out so that we don't have controllers utilizing it: http://pastie.org/3824571