Laravel Class 'App\Exceptions\Controller' not found - laravel-5

My question is that when I call route (post method) in group and localizationredirect is set, It return Class 'App\Exceptions\Controller' not found but when I call it outside the group it access successfully.
Please let me know that Is that possible to call the route (post method) in group.
Not working: http://localhost/lovebnb/en/join/upload-photo/33
Working: http://localhost/lovebnb/join/upload-photo/33

Related

How can I disable authentication for GET method in django modelviewset?

I have written one router url and modelviewset like below:
**router.register(r'TestViewSet', views.TestViewSet)
**class TestViewSet(viewsets.ModelViewSet): queryset = Test.objects.all() serializer_class = TestSerializer
for this class, I want to disable authentication for GET method.
Pls give some solution
Well to disable authentication only on the GET method of the ModelViewSet, we are required to override the permission class based on the HTTP method.
To achieve this, try following
from rest_framework import permissions
...
class TestViewSet(viewsets.ModelViewSet):
...
def get_permissions(self):
"""Returns the permission based on the type of action"""
if self.action == "list":
return [permissions.AllowAny()]
return [permissions.IsAuthenticated()]
In the above code, we are checking of the action (assuming you want to allow anyone to see the list, and limit who can perform other actions).
So if the action is list (HTTP method GET) it will allow anyone to access otherwise it will check for authentication.
Hope this answers your question.

Laravel 5 Unable to access currently logged in user id from custom helper

In AppServiceProvider, I called a function from a custom helper as follows:
public function boot()
{
View::share('tree', customhelper::generateSiteTree(0));
}
The custom helper file is calling the database function as below:
$children = UserPermission::getLeftNavByUserId($startAt);
In the custom helper function, I want to pass the current logged in user ID however, dd(Auth::user()) is returning null.
How can I pass the Auth::user()->id with the method
getLeftNavByUserId($startAt, Auth::user()->id);
The variable (or Facade) isn't available yet. One way to solve this is by using a view composer.
View::composer('my.view.with.children', function(View $view){
$view->with('children', UserPermission::getLeftNavByUserId($startAt, Auth::id()));
});
Ofcourse you need to add a check if the user is logged in or not etc.
Custom helper function will be initialized in application instance before the Auth middleware therfore it will always be null, if you want to use the auth user bind it from middlware instead.

Laravel: trying to make three update functions within one controller

I'm trying to make three different update functions in CompanyAdressController: defaultUpdate, contactUpdate and generalUpdate.
In first case I'm trying to access them via api:
from my js app:
this.$http.put('http://127.0.0.1:8000/api/companyDefault/' + this.hospital.default.id, this.hospital.default)
and inside api routes:
Route::resource('/companyDefault', 'CompanyAddressController#defaultUpdate');
and in CompanyAddressController:
public function defaultUpdate(Request $request, CompanyAddress $companyAddress)
{
...
}
I've got an error:
"message": "Method [defaultUpdate#update] does not exist on [App\\Http\\Controllers\\CompanyAddressController].",
"exception": "BadMethodCallException",
How should I correct my routes to get access to my method? Or should I do it different way by making one controller update function with parameters from my api function?
You're using resourceful controller check laravel documentation: https://laravel.com/docs/5.6/controllers#resource-controllers. The proper syntax for registering a resourceful controller is:
Route::resource('companyDefault', 'CompanyAddressController');
I think this is what you want:
Route::put('/companyDefault', 'CompanyAddressController#defaultUpdate');
Route::put('/contactUpdate', 'CompanyAddressController#contactUpdate');
Route::put('/generalUpdate', 'CompanyAddressController#generalUpdate');

Laravel - How to pass a variable to controller through resource route?

I've defined this resource in web.php of Laravel app,
Route::resource('Student', 'StudentCtrl');
and I have a variable which contains name of meta file,
$metaFile = 'student_meta.json';
I want to pass $metaFile to StudentCtrl, so I can catch the file name there.
I'm using Laravel 5.4.
Thanks.
Just pass the variable to the url, And thats it.
if its a get request will be routed to StudentCtrl#show
if its a put request will be routed to StudentCtrl#update
if its a delete request will be routed to StudentCtrl#destroy
And so on.
All you have to do is to define a method of the form. GET,POST,PUT/PATCH,DELETE.
Have a look here.
Passing a single variable from Router to Controller is possible only for Single HTTP request method.
Refer here : https://stackoverflow.com/a/50405137/9809983
For a complete resource route, its possible to send a fixed value to all views under that resource, if that's what you are trying to do
In Controller
public function __construct(Request $request)
{
$action_array = $request->route()->getAction();
$json_file['json_file'] = "student_meta.json";
$new_action_array = array_merge($action_array, $json_file);
$request->route()->setAction($new_action_array);
}
Now you can access the value in all the views under the resource controller like,
In View
{{ request()->route()->getAction('json_file') }}
Tested and perfectly works in Laravel 5.6

Pass data from routes.php to a controller in Laravel

I am attempting to create a route in Laravel for a dynamic URL to load a particular controller action. I am able to get it to route to a controller using the following code:
Route::get('/something.html', array('uses' => 'MyController#getView'));
What I am having trouble figuring out is how I can pass a variable from this route to the controller. In this case I would like to pass along an id value to the controller action.
Is this possible in Laravel? Is there another way to do this?
You are not giving us enough information, so you need to ask yourself two basic questions: where this information coming from? Can you have access to this information inside your controller without passing it via the routes.php file?
If you are about to produce this information somehow in your ´routes.php´ file:
$information = WhateverService::getInformation();
You cannot pass it here to your controller, because your controller is not really being fired in this file, this is just a list of available routes, wich may or may not be hit at some point. When a route is hit, Laravel will fire the route via another internal service.
But you probably will be able to use the very same line of code in your controller:
class MyController extends BaseController {
function getView()
{
$information = WhateverService::getInformation();
return View::make('myview')->with(compact('information'));
}
}
In MVC, Controllers are meant to receive HTTP requests and produce information via Models (or services or repositores) to pass to your Views, which can produce new web pages.
If this information is something you have in your page and you want to sneak it to your something.html route, use a POST method instead of GET:
Route::post('/something.html', array('uses' => 'MyController#getView'));
And inside your controller receive that information via:
class MyController extends BaseController {
function getView()
{
$information = Input::get('information');
return View::make('myview')->with(compact('information'));
}
}

Resources