Laravel + Inertia: switch rootView layout when redirecting - laravel

I'm using Laravel + Inertia + React in my project and I define the rootView layout in the HandleInertiaRequests middleware, depending on which route is requested, like this:
public function rootView(Request $request)
{
if ($request->route()->getPrefix() == "/admin") {
return "adm";
} else {
return "app";
}
}
But I found a problem when redirecting from a route with app rootView to a route with adm rootView (from /login to /admin).
The problem is that the rootView is not changed before or after the redirect. Is there a way to force the redirection to reload the full page?

I found the solution in this thread: https://stackoverflow.com/a/68609938/173299. The trick is add the following to the login request in AuthenticatedSessionController:
if ($request->session()->has("url.intended")) {
return Inertia::location(session("url.intended"));
}
I'm leaving my original question just in case anyone else find it useful.

Related

Laravel Fortify modify RegisterViewResponse

I am trying to modify the behavior of Fortify Register route.
I am sending out custom register urls, if this url doesn't match my logic I need to redirect to a custom page, so you can not even enter /register.
But I am only getting a 302 redirect loop for /register.
To do so I created a RegisterViewResponse to override default behavior:
use Laravel\Fortify\Contracts\RegisterViewResponse as RegisterViewResponseContract;
class RegisterViewResponse implements RegisterViewResponseContract
{
public function toResponse($request)
{
$canEnter = true;
if($canEnter){
return redirect()->intended('/register');
} else {
return redirect()->intended("/otherPage");
}
}
}
I also added this to FortifyServiceProvider:
$this->app->singleton(RegisterViewResponse::class,Responses\RegisterViewResponse::class)
Thanks for any help or advice!

Laravel nova - redirect from Dashboard

I would like to remove dashboard from my Laravel Nova app.
I found it easy to remove it from sidebar-menu - simply comment /views/dashboard/navigation.blade.php code.
However, I want to add a redirection logic (landing page depends on user role) so when navigating to / user will be redirected to a resource or tool which corresponds him.
(I have already implemented a redirection after login (https://stackoverflow.com/a/54345123/1039488)
I tried to do it with cards, but looks like this is not the right solution.
Any idea where can I place the redirection logic?
Nova 4; You can override the initialPath like so:
class NovaServiceProvider extends NovaApplicationServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
parent::boot();
Nova::initialPath('/resources/users');
}
// ...
}
This way, you get redirected to the Users resource upon logging in.
Pre nova 4 method:
To NovaServiceProvider.php add to boot method:
Nova::script('menuFix', __DIR__.'/../../resources/js/fixMenu.js');
Create file fixMenu.js with following:
if (location.pathname == '/' || location.pathname == '/dashboards/main'){
location.href = '/whereToRedirect'
}
A cleaner and safe way for Nova 3.x or below:
Copy vendor/laravel/nova/resources/views/layout.blade.php to resources/views/vendor/nova/
Now open resources/views/vendor/nova/layout.blade.php and edit it
Replace this line with the code below window.Nova = new CreateNova(config);
window.Nova = new CreateNova(config);
window.Nova.booting((Vue, router, store) => {
/** This fixes showing an empty dashboard. */
router.beforeEach((to, from, next) => {
if (to.name === 'dashboard.custom') {
next({ name: 'index', params: { resourceName: 'users'}});
}
next();
});
});
Replace users with your entity name's plural like products
Now save the file and refresh the nova dashboard and you should see the new page.
The solution was taken from here with clear steps.
The solution may also work for 4.x, but I haven't checked it yet.
Happy Coding :)
Just figured this out myself. In your Routes/web.php file, add a redirect route:
Route::redirect('/','/resources/{resource_name}');
where {resource_name} is the plural form of the resource. For example, '/resources/posts'.
In your case, you may want to redirect to your own control file, where the redirect logic can be placed.
Route::get('/', 'YourController#rootRedirectLogic');
Then in the controller YourController, add the method:
public function rootRedirectLogic(Request $request) {
// some logic here
return redirect()->route('YourRoute');
}
where 'YourRoute' is the name of the route you want to send the user to.
(Found clues to this solution in a comment by dillingham here: https://github.com/laravel/nova-issues/issues/393)
i came across this link : Laravel Nova - Point Nova path to resource page
Not sure it's a permanent solution but editing LoginController.php will do.
public function redirectPath()
{
return Nova::path().'/resources/***<resource_name>***;
}
**change to your own resource name

Laravel - Action not defined but it is defined

I get this error when i try loading blade.php
Action App\Http\Controllers\InventoryItemController#change not defined.
I have change function in InventoryItemController
public function change($new_status)
{
//
}
This started when I wanted to make button
Confirm Change
I did everything same when i made Edit button and that button works normally.
UPDATE 1
My button looks like this now
<a href="{{route('change', [$inventoryitem['new_status'],
$inventoryitem['asset_id']])}}"class="btn btn-info">Confirm Change</a>
and my change function is this
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
return redirect('showasset', compact('asset','asset_id'));
}
and my route in web is like this
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
But after i click button it just redirect me to url .../change/4/1 and that's it. Nothing changes.
Using Action is deprecated in Laravel
You can use routes instead.
Define Routes in your routes files (/routes/web.php) like.
Route::get('change/{status}','InventoryItemController#change')->name('change');
and then in your view
Confirm Change
In your controller use.
public function change ($status){
// rest of the function.
}
Hope this helps
Define your controller's method in route file as following:
Route::get('url/{new_status}',InventoryItemController#change);
Answer on UPDATE 1
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
$asset->save();
return view('your_view_path',compact('variable1','variable2'));
}
Final error was in my route
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
It should be like this
Route::get('change/{new_status}/{asset_id}','InventoryItemController#change')->name('change');
After that change everything is working flawlessly. Thank you for your help guys!

Handle URL in CodeIgniter

I have a url like
"www.site.com/index.php/details/productname"
and I get 404 error on that link in codeigniter but the URL www.site.com/index.php/details/ is working fine. I want handle url parameter in seo manner.
You have to add /index/ segment in part of your url:
http://www.site.com/index.php/details/index/productname/
If you want to open url such like that http://www.site.com/index.php/details/productname/:
1) you need to define _remap() method:
public function _remap($method)
{
if ($method == 'index')
{
$this->viewDetails($this->uri->segment(2));
}
else
{
$this->default_method();
}
}
OR
2) use application/config/routes.php file
$route['details/(:any)'] = "products/viewDetails/$1";
From User-Guide:
routers.php
_remap method

Why my code igniter controller works partially?

I have enabled the query string settings and works fine for all the query string related URL's but except for this login controller... when I try like this, it gives me an error,
404 Page Not Found
The page you requested was not found.
URL : www.test.com/login/?sType=1
but when I access without query string, www.test.com/login/ ... works fine...
similarly other query string urls working fine as below,
http://www.test.com/core/child/register/?sIds=3,4,&type=1
etc.,
How can I fix this issue?
My login controller,
function Login()
{
parent::Controller();
$this->load->model("user_login_model");
$this->load->library('user_agent');
}
// to load the login screen
function index()
{
//$this->load->model("user_login_model");
$this->load->view('login/user_login_form');
}
// to show as login
function show()
{
$this->load->view('login/user_login_form');
}
// to process the login
function checkuser()
{
}
// to logout
function logout()
{
}
}
/* End of file welcome.php /
/ Location: ./system/application/controllers/welcome.php */
I have also experienced some problems in the past when there is only one element in query string. Try this:
www.test.com/login/?sType=1&n=1
It might work.

Resources