I have below configuration in my Laravel /routes/web.php:
Route::group(['prefix' => 'admin'], function(){
Route::get('/', function() {
return view('admin.login');
});
});
If you observe, I have mentioned view('admin.login') this calls /resources/views/admin/login.blade.php. Which holds good as of now.
But for this Route group, I will have all my views inside /resources/views/admin. Thus, I do not want to use admin before every view-name.
Is there any possible parameter at Route::group level by which I can define namespace of my views, so that the Laravel searches my views in the particular directory inside /resources/views/?
I faced the same problem and created a helper function it worked ...
added a helpers directory under app directory app\Helpers\functions.php
function adminView($file){
return view('foo.' . $file);
}
in composer.json file registered this file
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": ["app/Helpers/functions.php"]
},
just run composer dump-autoload and you can use this helper function
You can use
View::addNamespace('admin', '/path/to/admin/views'); or View::addLocation('/path/to/your/admin/views');
to specify your admin view folder in your route file.
with the first method you can use
return view('admin::view.name'); and with the second method you can use view name directly like return view('view.name');
Related
Please, don't talk to technical in the answers:-D I am not a hardcore programmer.
What is a good way to store certain functions in Laravel? I have functions that apply on a "post" only or "media" only, like getAttributeList or getComponents. I say "Post" and "Media" because both have their own controller, model and views. It feels wrong to put it in the model because that should be database stuff right? And traits are more for recurring functions all over the place, right? So, right now I have one big file called Helpers.php. And uh, it is getting large... should I simply separate it in PostHelpers.php, MediaHelpers.php etc? Or is there a more elegant way in Laravel to do it?
It is quite simple : Just check your composer.json file at root directory of ur app. and under autoload section add :
"autoload": {
"psr-4": {
"App\\": "app/"
},
"files": ["app/helper.php"],
"classmap": [
"database/seeds",
"database/factories"
]
"files": ["app/helper.php"], This is the line you need to add in ur composer file and provide the path to file .
In my case i have created a file helper.php in App directory where i keep all my functions .
after this run this command :
composer dump-autoload
Now u can access your functions anywhere.
In your composer json file check this snippet
"autoload": {
"files": [
"app/Helpers/global_helper.php"
],
As you see I have auto loaded 1 single file called global_helper.php in a folder called Helpers Now in this file I have a function called loadHelper(...$files)
What this function does is
if (!function_exists('loadHelper')) {
function loadHelper(...$file_names)
{
foreach ($file_names as $file) {
include_once __DIR__ . '/' . $file . '_helper.php';
}
}
}
You can pass your file name as array or string and it will include those files into your Controller constructor
So In my Controller whenever I want some helper function I create a saperate helper file for that controller then in constructor i ust include it.
I am not sure if there is any better solution but so far this is how I am making all my projects .
I hope this will help you ;)
I have controllers in different folder than Laravel native App\Http\Controllers. I am using a custom Lib\MyApp folder which has modules inside. Each module has its own controllers, models etc. I added to composer.json autoloading to app\lib.
What I did is change RouteServiceProvider namespace:
protected $namespace = 'App\Lib\MyApp';
I did a composer dump-autoload after everything.
Inside MyApp is a Landing\Controller folder with actual controller class inside.
Try 1 (ideal):
I would like to call my route like this:
Route::get('/', 'Landing\Controller\LandingController#index');
But this way I am getting a ReflectionException that the class is not found even though
Try 2:
Route::get('/', '\Landing\Controller\LandingController#index');
Trailing slash gets rid of the namespace part when I refresh the page, and class is still said not to exist.
Try 3:
Route::get('/', 'MyApp\Landing\Controller\LandingController#index');
This just duplicates MyApp folder, and class is not found as expected.
Try 4 (working, but don't want it like that)
Route::get('/', '\MyApp\Landing\Controller\LandingController#index');
This works fine, although I would like to get rid of the \MyApp\ part.
Is something like this possible?
You can use the namespace in the routes for that purpose :
Route::namespace('Landing\Controller')->group(function () {
Route::get('/', 'LandingController#index');
// + other routes in the same namespace
});
And dont forget to add the namespace to the controllers :
<?php namespace App\Lib\MyApp\Landing\Controller;
PS : in the case where the Lib is inside the App folder there is no need to add a thing in the composer file, because the App folder is registred in the psr-4 and with this it will load all the files within this namespase for you.
There are many ways to add the namespace in Laravel
Route::group(['prefix' => 'prefix','namespace'=>'Admin'], function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
Route::namespace('Admin')->group(function () {
// your routes with"App\Http\Controllers\Admin" Namespace
});
//single route
Route::namespace('Admin')->get('/todo', 'TaskController#index');
//single route
Route::get('/todo', 'Admin/TaskController#index');
// by ->namespace
Route::prefix('admin')->namespace('Admin')->group(function () {
// route code
});
For "laravel 8"
Here I have given an example with both namespace and prefix but you can also use any one according to your requirement.
I created Controller in Controllers dir with command
php artisan make:controller Admin/StoriesController
Route::namespace('Admin')->prefix('admin')->group(function(){
Route::get('/deleted_stories',
'\App\Http\Controllers\Admin\StoriesController#index')->name
('admin.stories.index');
});
I use Laravel framework and this is my current directory:
As you see, there is a class named Log (the one I've selected). Now I need to make it global. I mean I want to make it accessible in everywhere and be able to I make a object (instance) of it in following files:
All files of classe folder
All controller
web.php file of
All file of views
Anyway I want to be able to make a instande of it and call its methods everywhere like this:
$obj = new Log();
$obj->insert($message);
How can I do that?
You can create global Laravel helper:
if (! function_exists('log')) {
function log($message)
{
(new Log)->insert($message);
}
}
Put it in helpers.php and add this to composer.json to load the helpers file:
"autoload": {
....
"files": [
"app/someFolder/helpers.php"
]
},
Then you'll be able to use this helper globally:
log('User added');
In views:
{{ log('User added') }}
Update
#stack, you're using wrong syntax for JSON (screenshot in comments), here's correct one:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Helpers/helpers.php"
]
},
I'm new to laravel.
This might be very simple but i was unable to find an example or documentation.
I need to redirect the user to an action in a controller that is in a sub folder.
Folder structure:
**app**
---**controllers**
------**Admin**
---------AdminHomeController.php (extends AdminController)
------AdminController.php
------BaseController.php
---**models**
---**views**
------**admin**
---------dashboard.php
------login.php
Routes.php
Route::get('/login', function()
{
return View::make('login');
});
Route::group(array('before' => 'auth'), function()
{
Route::resource('admin', 'AdminHomeController');
});
Route::post('/login', function()
{
Auth::attempt( ['email' => Input::get('email'), 'password' => Input::get('password')] );
**return Redirect::action('AdminHomeController#showAdminDashboard');**
});
After login i'm wanting to redirect to the action in AdminHomeController called "showAdminDashboard".
I know i could just load the view but i'm wanting to redirect.
My error is this - Unknown action [AdminHomeController#showAdminDashboard].
When you create new classes for things like controllers you'll need to dump your Composer autoload file again so that the classmap can be updated. If you open composer.json you should see a classmap key and the value will be an array of directories. One of the listed directories will be app/controllers.
Laravel doesn't know about your controller until your dump a new autoload. From a terminal simply run composer dump-autoload, it will take no longer then a couple of seconds.
Unless you namespace the controller, I think what you have to do is use an underscore to reference the controller in the route:
Route::group(array('before' => 'auth'), function()
{
Route::resource('admin', 'Admin_AdminHomeController');
});
Then make sure to rename the AdminHomeController class:
class Admin_AdminHomeController extends AdminController {
Leave the file name the same "AdminHomeController.php" and leave it inside the "Admin" folder. After that, run composer dump-autoload again, and I think you'll be working.
How can I create admin specific routes in Laravel 4 (Restfull Controllers):
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
What Files and where I need create theam
How I need create the route
In Laravel 4 you can now use prefix:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#home');
Route::get('posts', 'AdminController#showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
#Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
Route::resource('admin/users', 'admin.Users');
or
Route::resource('admin', 'admin.Users');
thanks
Really useful tool that you can use is the artisan CLI.
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
Route::resource('users', 'Users');
This'll setup all the necessary routes.
For more information on this you should read the documentation at.
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
Edit:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
http://four.laravel.com/docs/routing#route-filters
Laravel 4 - Add Admin Controller Easily
This was driving me insane for ages, but i worked it out.
routes.php
Route::resource('admin', 'Admin_HomeController#showIndex');
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController#showIndex');
});
Thanks
Daniel