Jump to content
modifiedcontent

Redirect home after logout

Recommended Posts

I think you can create a simple logout link with

<a href="admin/login/logout">log out</a>

But it redirects to the login form in the admin area. Can I hook into logout and change the redirect? Default redirect home would make sense.

Or is the way to do this still to create a logout template + page with session->logout()  and a redirect?

Share this post


Link to post
Share on other sites

You can hook before ProcessLogin::executeLogout and set the desired url.

<?php

wire()->addHookBefore("ProcessLogin::executeLogout", null, "setRedirect");

function setRedirect(HookEvent $event) {
	$event->object->setLogoutURL($pages->get('/')->httpUrl);
}

 

 

  • Like 4

Share this post


Link to post
Share on other sites

Thanks BitPoet.

I get server errors when I put this in ready.php.  - should it go there or could you also put it in a functions.php file within the templates folder? This is the error I get on logout:

Error: Uncaught Error: Call to a member function get() on null in /var/www/html/site/ready.php:6
Stack trace:
#0 /var/www/html/wire/core/WireHooks.php(777): setRedirect(Object(ProcessWire\HookEvent))
#1 /var/www/html/wire/core/Wire.php(402): ProcessWire\WireHooks->runHooks(Object(ProcessWire\ProcessLogin), 'executeLogout', Array)
#2 /var/www/html/wire/core/ProcessController.php(244): ProcessWire\Wire->__call('executeLogout', Array)
#3 /var/www/html/wire/core/Wire.php(374): ProcessWire\ProcessController->___execute()
#4 /var/www/html/wire/core/WireHooks.php(698): ProcessWire\Wire->_callMethod('___execute', Array)
#5 /var/www/html/wire/core/Wire.php(402): ProcessWire\WireHooks->runHooks(Object(ProcessWire\ProcessController), 'execute', Array)
#6 /var/www/html/wire/core/admin.php(113): ProcessWire\Wire->__call('execute', Array)
#7 /var/www/html/wire/modules/AdminTheme/AdminThemeReno/controller.php(13): require('/var/www/html/w...')
#8 /var/www/html/site/templates/admin.php(15): require('/ (line 6 of /var/www/html/site/ready.php) 

This error message was shown because: you are logged in as a Superuser. Error has been logged.

This version produces no errors, but doesn't do anything either:

wire()->addHookBefore("ProcessLogin::executeLogout", null, "setRedirect");

function setRedirect(HookEvent $event) {
	$event->object->setLogoutURL('/');
}

Probably unrelated, but does it make any difference if you do wire()->... or wire->... ?

Why is redirect to homepage not default behavior anyway?! The login form in the admin area is the last place you want to be when you log out. Wordpress has the same annoying illogical default behavior. Is there some obscure reason why it makes sense?

Share this post


Link to post
Share on other sites

This should work:

    wire()->addHookBefore("ProcessLogin::executeLogout", null, "setRedirect");

    function setRedirect(HookEvent $event) {
        $event->object->setLogoutURL(wire('pages')->get('/')->httpUrl);
    }

 

  • Like 2

Share this post


Link to post
Share on other sites

Thanks adrian !

Yes, that does seem to work.

I have seen another hooked function like this:

$wire->addHookAfter ...

Is wire() and $wire and wire-> etc. all the same? What's what? I guess I'll have to study up on that...

And would this work within the templates folder, in a functions.php or something like that? I'll have to try that...  The ready.php file is awkward.

Share this post


Link to post
Share on other sites
2 hours ago, modifiedcontent said:

The ready.php file is awkward.

Why? Ready.php is my best friend :)

  • Like 1

Share this post


Link to post
Share on other sites
3 hours ago, modifiedcontent said:

Is wire() and $wire and wire-> etc. all the same? What's what? I guess I'll have to study up on that...

Useful posts:

 

  • Like 2

Share this post


Link to post
Share on other sites

adrian's solution works well, but causes an internal server error on one page that loads an external script:

2017-04-02 07:06:03	guest	http://mysite.com/http404/	Error: 	Cannot redeclare setRedirect() (previously declared in /var/www/html/site/assets/cache/FileCompiler/site/ready.php:5) (line 5 of /var/www/html/site/assets/cache/FileCompiler/site/ready.php)

Renaming the function did not fix it. Don't have time to dig into this right now. Leaving this note here and will report back...

Share this post


Link to post
Share on other sites
On 27/03/2017 at 5:25 PM, BitPoet said:

You can hook before ProcessLogin::executeLogout and set the desired url.


<?php

wire()->addHookBefore("ProcessLogin::executeLogout", null, "setRedirect");

function setRedirect(HookEvent $event) {
	$event->object->setLogoutURL($pages->get('/')->httpUrl);
}

 

Works well if you change null for $this. Was erroring before.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By picarica
      So hello i am trying to get a .png file from file field and put it automatically to image field, why png image is in the file field is because i already have a hook that extracts .zip and uploads all content into file field, but i just realizes i cant use size() function on image in file field so i am tryin got reupload it to images field
      i already have something like  this in ready.php
      $word = ".png"; foreach($page->subor_hry as $file) { if(strpos($file, $word) !== false){ $page->images_thumb = $file->url; } } by my logic it should work but it dosnt i get error ProcessPageEdit: Unable to read: /site-hry/assets/files/1027/flash_fishy_screenshot.png
      when i remove url from $file->url i just get ProcessPageEdit: Item added to ProcessWire\Pageimages is not an allowed type
      so what am i doing wrong? is there some other way to do this ?
      also can i have all this in
      $this->addHookAfter('Pages::saveReady', function(HookEvent $event) { whats the correct function to have it apply on all pages ?
    • By rooofl
      Hi! I am trying to create a hook that takes the value of a form file field, and add it to an image gallery of an existing page.
      $forms->addHookBefore('FormBuilderProcessor::processInputDone', function($e) { $form = $e->arguments(0); if($form->name == 'new-inuse') { // related_font is a page selector $font = $form->getChildByName('related_font'); // getting the page name $fontName = $font->attr('value')->name; // finds the page from its name $fontPage = wire('pages')->find("name=$fontName"); // in_use_image is a file upload field $image = $form->getChildByName('in_use_image'); // trying to add the image to the existing image gallery in_use field $fontPage->in_use->add('$image'); // error here $fontPage->save(); } }); This outputs the following error: `Call to a member function add() on null`. Any idea what’s wrong with my code?
    • By jploch
      Hey folks,
      currently Iam working on a website for one of my clients and I need some advice on how to approach this in PW.
      The website is for a company, that offers holiday houses in two locations. 

      The client wants the homepage to show the first location. Normally I just have a home template for the first page, but here the URL should reflect that you are in Location 1. So when you visit the URL casamani.com it should redirect to casamani.com/location-1. Not sure if this makes sense at all.

      Whould it be bad for SEO and performance reasons to redirect home to the Location-1 page?
      Another approach would be to render the Location-1 template on the home template or do an include like discussed here.
      Here is how the tree looks:
      – Home
      – Location 1 (Homepage)
           – Creation
           – Adventure
           – Sustainability
      – Location 2 
           – Creation
           – Adventure
           – Sustainability

      Thanks for looking into this!
    • By picarica
      so what i am trying to do is that i uploded some files into file field, and then i want hook to get MD5sum and other stuff from .xml and put it into text field into it coresponding pages, pages like this are gonna be many, for each one i want it to output it into its fields
    • By Paul Greinke
      Hi there. I wrote a custom module for one of my projects. In fact I maybe want to use my module in other projects too. In order to be variable and customizable  I need to implement some custom hooks into my module. So I can afterwards hook into the my functions in order to modify them to match the needs of the new project.
      I tried simply defining functions with the '__' prefix. But that did not work. I'm imagining something like the following:
      <?php class MyClass { public function ___someFunction() { // Do something } } // ready.php $this->addHookBefore('MyClass::someFunction', function($event) { // some customization }); Is there a way to accomplish that? 
×
×
  • Create New...