Redirect to nested controller action - laravel-6

I recently moved my controllers to an Admin directory.
I changed the namespace: namespace App\Http\Controllers\Admin;
I have included the Controller class: use App\Http\Controllers\Controller;
In my controller, I have a redirect to the controller's index() action.
return redirect()->action('ServiceController#index');
Now I get the following error:
InvalidArgumentException Action
App\Http\Controllers\ServiceController#index not defined.
I can't figure out how to declare the new action redirect in the docs so I am posting my question here.
Routes
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function () {
Route::resource('projects', 'ProjectController');
Route::resource('services', 'ServiceController');
Route::resource('projectFiles', 'ProjectFileController');
Route::get('seed', 'SeedController#seedDatabase')->name('seed');
});
This is the part of the controller where I am talking about:
class ServiceController extends Controller
{
public function index()
{
return view('admin.services.index', [
'services' => Service::all()
]);
}
public function create()
{
return view('admin.services.create');
}
public function store(Request $request)
{
try {
Service::create([
'name' => $request->name,
'machine_name' => snake_case($request->name),
'description' => $request->description
]);
return redirect()->action('\App\Htpp\Controllers\Admin\ServiceController#index');
} catch (\Throwable $th) {
throw $th;
}
}
}

I think I found the answer, but anyone can correct me if I am wrong.
In RouteServiceProvider the namespace is set to App\Http\Controllers:
protected $namespace = 'App\Http\Controllers';
So I decided to add Admin\ before the name of the controller and now the redirect works:
return redirect()->action('Admin\ServiceController#index');

Related

Piranha CMS middleware to capture requests to /uploads

When a request is made for an image|file it doesn't reach my middleware.
I can see that when UseCms is called it adds PiranhaStartupFilter
serviceBuilder.Services.AddTransient<IStartupFilter, PiranhaStartupFilter>();
Which adds UseStaticFiles() to the ApplicationBuilder.
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return builder =>
{
builder
.UseSecurityMiddleware()
.UseStaticFiles()
.UseMiddleware<RoutingMiddleware>()
.UseMiddleware<SitemapMiddleware>();
next(builder);
};
}
How could I overwrite this functionality so that my middleware is called for requests?
I'm expecting a call to /uploads/foo.jpg would be picked up in the InvokeAsync method of my middleware, registered like so:
app.UsePiranha(options =>
{
options.Builder.CustomImageTools();
});
At present only files & favicon requests reach the InvokeAsync method in my middleware.
As middleware in asp.net is executed in the order they are added into the pipeline it should be sufficient to add your interceptor before making any calls to Piranha.
You can add the middleware by adding a service above the call to UseCms() in startup.cs.
services.AddPiranha(options =>
{
options.CustomImageFilter();
options.UseCms();
}
public class CustomImageFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UseWhen(
context => some_condition,
appbuilder => appbuilder.CustomImageTools()
);
// Call the next configure method
next(app);
};
}
}

Problems with login and authentication in Laravel

I need help with my login and authentication of admin.
In the database I have a table called 'admins' with columns of 'name', 'surname', 'password' in my native language.
Every time I press the login button when I try to log in, I get an error:
"Undefined index: password"
where password is in English in folder:
C:\wamp\www\app\vendor\laravel\framework\src\Illuminate\Auth\EloquentUserProvider.php
and I don't know why.
My custom controller AuthController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash;
use Session;
use App\Models\Admin;
use Illuminate\Support\Facades\Auth;
class AuthController extends Controller
{
public function index()
{
return view('auth.login');
}
public function customLogin(Request $request)
{
$request->validate([
'name' => 'required',
'surname' => 'required',
'passw' => 'required',
]);
$credentials = $request->only('name', 'surname', 'passw');
if (Auth::attempt($credentials)) {
return redirect()->intended('');
}
return redirect("login")->withSuccess('Wrong input data.');
}
public function dashboard()
{
if(Auth::check()){
return view('');
}
return redirect("login")->withSuccess('Wrong input data.');
}
public function signOut() {
Session::flush();
Auth::logout();
return Redirect('');
}
}
My route:
Auth::routes();
Route::post('/login', 'AuthController#customLogin');
I consulted with an acquaintance that specialises in web-programming and she said I should do a custom AuthController, which I did, but the problem is either still not fixed or this is a different error.
And from web sources I used:
https://www.positronx.io/laravel-custom-authentication-login-and-registration-tutorial/

How to make an admin ajax call in prestashop 1.7.6

I'm trying to make an ajax call in Prestashop Admin:
I created a module without a config page. It just add a button in some backoffice page, I'm trying to make an ajax call to my module file without success.
Making an ajax call in frontend is working (I added an ajax.php file in my modules/mymodule/controller/front/ directory), I tried to do the same thing for admin but it's not working at all.
What I've done:
loading the js file from actionAdminControllerSetMedia is ok
adding this in the composer.json file:
"autoload": {
"psr-4": {
"MyModule\\Controller\\": "controllers/admin/"
},
"config": {
"prepend-autoloader": false
},
created the controllers/admin/ajax.php file with this code (based on this documentation code):
namespace MyModule\Controller;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
class DemoController extends FrameworkBundleAdminController
{
public $auth = false;
public $ssl = true;
public $ajax = true;
public $errors = false;
public $message;
public function __construct()
{
parent::__construct();
}
public function initContent()
{
parent::initContent();
}
public function postProcess()
{
PrestaShopLogger::addLog("MODULE CONTROLLER OK ", 1);
}
public function displayAjax()
{
$this->ajaxDie(json_encode(array('success'=> !$this->errors, 'message' => $this->message)));
}
}
Then I tried to call the ajax from different way in js but never worked (the post query return is a message from prestashop "page not found" with http 200 response.
the doc isn't very helpful and I only find old messages/ways to do (from Prestashop 1.7.5 I'd be able to create a custom Admin controller but it doesn't work), can someone explain me the steps to follow?
thanks
Assuming it is for a PS1.7+ module, using Symphony:
Declare a link in a method of your admin controller (src/Controller/Admin) e.g
$adminLink = $this->generateUrl()
and return in with:
return $this->render
In your views/js/back.js"
$.ajax({
url: adminLink,
type: 'POST',
async: false,
data: {
},
success: (data) => {
}
});
Note: check the generateUrl and render functions for the necessary arguments.

CakePHP3 Auth redirectURL route broken

I have a controller with a particular method to login:
public function login() {
if ($this->request->is('post')){
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
// not logged
$this->Flash->error('Your username or password is incorrect');
}
}
and default route looks like
Router::scope('/', function (RouteBuilder $routes) {
$routes->fallbacks(DashedRoute::class);
});
after user is logged in CakePHP throws an error
Error: A route matching "/" could not be found.
None of the currently connected routes match the provided parameters.
Add a matching route to config/routes.php
when IMO it should to redirect to the page (based on a related controller) from where login method was executed.
Login code is based on that tutorial.
Any thoughts?
To solve this issue:
Please update the below lines in routes.php file
Router::defaultRouteClass('DashedRoute');
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'users', 'action' => 'index']);
$routes->fallbacks('DashedRoute');
});
Plugin::routes();
Please do create index() in users controller.
Let me know if any issue.

Laravel 4 Auth Filter: Unable to generate a URL for the named route as such route does not exist

I am trying to restrict a resource that I have named Artists (run by an ArtistsController). I tried doing this directly with the constructor in the controller:
public function __construct()
{
$this->beforeFilter('auth', array('except' => array()));
}
And in my filters, I have:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('fans/landing');
});
In my routes, I have:
Route::get('fans/landing', array('uses' => 'FansController#getIndex'))->before('guest');
However, when I run this (trying to go to one of the resource pages), I get the following error:
Unable to generate a URL for the named route "fans/landing" as such route does not exist.
This is strange, because when I remove the construct function, the fans/landing page loads fine. Also, it redirects another page (not part of the resource), fine to fans/landing, when I have:
Route::get('/fans/home', array('uses' => 'FansController#getHome'))->before('auth');
change
Route::get('fans/landing', array('uses' => 'FansController#getIndex'))->before('guest');
to
Route::get('fans/landing', array('as' => 'fans.landing', 'uses' => 'FansController#getIndex'))->before('guest');
and change
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('fans/landing');
});
to
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('fans.landing');
});